How to add an UINavigationController to an UIViewController presented as Modal

萝らか妹 提交于 2019-12-11 05:23:25

问题


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



来源:https://stackoverflow.com/questions/15058644/how-to-add-an-uinavigationcontroller-to-an-uiviewcontroller-presented-as-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!