开发者

Programmatically call navigation controller back button on iOS

开发者 https://www.devze.com 2022-12-17 11:09 出处:网络
In a UINavigationController-based iPhone app, in a method I would like to perform the programmatic equiva开发者_如何学Pythonlent of the back button being pressed and going back a view.

In a UINavigationController-based iPhone app, in a method I would like to perform the programmatic equiva开发者_如何学Pythonlent of the back button being pressed and going back a view.

i.e. automatically press the Jobs button as seen here:

Programmatically call navigation controller back button on iOS

Is there a generic iOS call I can make, or is more information required?


UINavigationController's -popViewControllerAnimated: method should do what you want:

[navigationController popViewControllerAnimated:YES];


Assuming you don't actually want to PRESS the button programmatically, but simply copy the outcome of pressing the button, you should tell the navigation controller to pop the current view controller.

[self.navigationController popViewControllerAnimated:YES];

This will remove it from the stack, and return you to the previous view controller.


Swift 3.0

Back to the root view

self.navigationController?.popToRootViewController(animated: true)

Back to the previous view

self.navigationController?.popViewController(animated: true)

Swift 2.3

Back to the root view

self.navigationController?.popToRootViewControllerAnimated(true)

Back to the previous view

self.navigationController?.popViewControllerAnimated(true)


You should call

popViewControllerAnimated:

which is the opposite of adding view controllers with pushViewController:animated:


[self.navigationController popViewControllerAnimates:YES];

is the best option but if you are nor on the same view controller class or your delegate changes before your back button method called then you can also try--

first you have to define back button---

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"anyTitleForBackButton" style: UIBarButtonItemStyleBordered target: nil action: @selector(backButtonTapped)];

[[self navigationItem] setBackBarButtonItem: newBackButton];

[newBackButton release];

and then in backButtonTapped method you can call--

[self.navigationController pushViewController:desiredViewController animated:YES];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号