0

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 ?

Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
The iOSDev
  • 5,237
  • 7
  • 41
  • 78

3 Answers3

2

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; 
Craig Mellon
  • 5,399
  • 2
  • 20
  • 25
1

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 :

  1. as the example above using presentModalViewController:

  2. 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.

Malek_Jundi
  • 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
1

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]; 
}
The iOSDev
  • 5,237
  • 7
  • 41
  • 78