简体   繁体   中英

How to add an UINavigationController to an UIViewController presented as Modal

I have an alternative flow in my app. This flow starts in my firstViewController, then in this view a call my secondViewController like this:

- (IBAction)PressButton:(id)sender {

    SecondViewController *second = [[SecondViewController alloc] init];
    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    UINavigationController *nav = self.navigationController;
    [nav presentViewController:second animated:YES completion:nil];                              
}

In my secondViewController I want to push my thirdViewController. But it is not working I tried this ways:

- (IBAction)pressButton:(id)sender {

   ThirdViewController *tvc = [[ThirdViewController alloc] init];
   UINavigationController *nav = self.navigationController;
   [nav pushViewController:tvc animated:YES];

}

When I press the button of secondViewController nothing happens.

What I'm doing wrong ?

I'm using:

  • OSX 10.8.2
  • Xcode 4.6
  • iOS 6.1

You must present the navigation controller modally, and have the second view as the root of that navigation controller. As well as calling presentViewController from the owning view not its parent navigation controller.

- (IBAction)PressButton:(id)sender {
        SecondViewController *second = [[SecondViewController alloc] init];
        second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:second];
        [self presentViewController:navigationController animated:YES completion:nil];    
    }

Instead of presenting just the second view controller, make sure to present an additional navigation controller.

SecondViewController *secondViewController = [[SecondViewController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[[self navigationController] presentViewController:navigationController animated:YES completion:nil];

If you are using storyboard just click on the source view xib, Ctrl+drag to destination (Create Segue), select modal from popup menu.Click on the newly created connection. Add a name for it then in source view controller [self performSegueWithIdentifier:@"Segue Name" sender:self];

If you are trying to get a button to direct you to a different page modally, you can go into the storyboard or xib file. Control click from that button to the view controller you want to go to. and then the popup menu will give you the options of what type of outlet you want to use. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM