简体   繁体   中英

Programmatically call navigation controller back button on iOS

In a UINavigationController-based iPhone app, in a method I would like to perform the programmatic equivalent of the back button being pressed and going back a view.

ie automatically press the Jobs button as seen here:

导航控制器图像

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

UINavigationController-popViewControllerAnimated:方法应该做你想要的:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM