How to add an UINavigationController to an UIViewC

2019-08-01 09:13发布

问题:

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

回答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];    
    }


回答2:

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];


回答3:

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];



回答4:

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