I first like to load simple viewController which shows some option and then clicking on some button I would like to load navigationController or tabbarController depending on button click. How can I do this ?
3 Answers
I replace the root view controller on the window when I want to switch the views.
For example in my app I show a loading screen first then I switch the view to a login screen.
To do this you need a reference to your app delegate then you can access the window property and replace the root view controller:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
LoginViewController *loginVC = [[LoginViewController alloc] init];
appDelegate.window.rootViewController = loginVC;

- 5,399
- 2
- 20
- 25
-
but in ios 5 i got error when i try to use first line of your answer's code block – The iOSDev Apr 25 '12 at 09:19
-
Replace MyAppDelegate with the name of your app delegate file. MyAppDelegate is the name of my file, yours will be different – Craig Mellon Apr 25 '12 at 09:25
-
Default for a new project is called AppDelegate. I've updated my code block – Craig Mellon Apr 25 '12 at 09:26
-
i got your intention and change accordingly to my project see my answer below but thanks for your idea. – The iOSDev Apr 25 '12 at 10:45
In your simpleViewController :
- (IBAction) yourButtonAction:(id)sender
{
UIViewController *Vc = [[theViewControllerYouWantToShow alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:Vc];
[self presentModalViewController:nav animated:YES];
}
Edit :
you have three options to show your viewController content :
as the example above using
presentModalViewController:
add the viewController view as a subView to the current viewController. in your case :
[simpleViewController.view addSubView:nav.view];
3.or if your simple ViewController is the navigation root viewController you can push other viewControllers to its navigation stack.

- 6,140
- 2
- 27
- 36
-
i dont what to use presentModelviewController as i need to do some more process and my app need to change viewControllers many time – The iOSDev Apr 25 '12 at 08:49
-
i first present simple viewController on that viewController's view i have three buttons on clicking the button i want to load either navigationViewController or TabBarViewController – The iOSDev Apr 25 '12 at 09:22
in appdelegate.h
@property (strong, nonatomic) id<UIApplicationDelegate>delegate;
in appdelgate.m
@synthesize delegate;
in my first viewController's .h file
AppDelegate *myappDelegate;
-(IBAction)start:(id)sender;
in my first viewController's .m file
-(IBAction)start:(id)sender
{
NSLog(@"Start Button is clicked");
mvc = [[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
myappDelegate = [[UIApplication sharedApplication]delegate];
myappDelegate.navigationController = [[UINavigationController alloc]initWithRootViewController:mvc];
myappDelegate.window.rootViewController = myappDelegate.navigationController;
[myappDelegate.window makeKeyAndVisible];
}

- 5,237
- 7
- 41
- 78