Navigating back to main ViewController dismissView

2019-02-27 07:47发布

I have 2 ViewControllers directly connected with a push segue. I am navigating from first to second view controller by calling [self performSegueWithIdentifier:@"segueIdentifier" sender:sender]. On the second one I have an IBAction method that is bound to a "Done" button. Pressing that button should basically cause the first view controller to be displayed (sort of a back button). I managed to do that with:

NSArray *viewControllers = self.navigationController.viewControllers;
[self.navigationController popToViewController:[viewControllers
objectAtIndex:0] animated:YES];

I did try to achieve the same effect by using:

[self dismissViewControllerAnimated:YES completion:nil];

No matter what I tried though this didn't do the job. I am trying to understand what exactly am I missing but I can't figure it out. Does dismissViewControllerAnimated method work only with Modal segues ( this is the only thing that came to mind ).

Thank you

1条回答
The star\"
2楼-- · 2019-02-27 07:54

Yes,

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion

is when a UIViewController is displayed modally.

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

should do what you are seeking.

So basically, in your second VC:

[self.navigationController popViewControllerAnimated:YES];

You will save you a lot of trouble if you read the UIViewController and UINavigationController references. Twice ;)

查看更多
登录 后发表回答