Navigation Controller Back button to go to the screen before the previous screen

左心房为你撑大大i 提交于 2019-12-10 13:09:34

问题


I'm wondering whether it's possible to go back to 2 previous page using navigation controller button. Right now, I've custom made the button using this code

self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:mainLib.navCountryTitle
                                 style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(handleBack:)];

And on 'handleback' method, I called this line

[self.navigationController popViewControllerAnimated:YES];

So in this case it will go to the previous screen. However I want to go to the screen before the previous screen.

I know that i can call that screen straight away, but I wouldn't want to do that because in every screen I have a back button which supposed to go to its previous screen.


回答1:


You can pop to particular viewcontroller using its pushing index. If you know the syntax than you can do that like

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:indexOfViewController] animated:YES];

I believe, In your case it would be 1st Index. Though you can again change index and check.

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

Here self.navigationController.viewControllers is the array of the all the ViewControllers pushed.

Hope it helps.



来源:https://stackoverflow.com/questions/5752297/navigation-controller-back-button-to-go-to-the-screen-before-the-previous-screen

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