87

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.

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

Navigation Controller image

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
oberbaum
  • 2,451
  • 7
  • 36
  • 52

5 Answers5

190

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

[navigationController popViewControllerAnimated:YES];
Pang
  • 9,564
  • 146
  • 81
  • 122
Steve Harrison
  • 121,227
  • 16
  • 87
  • 72
  • 12
    awesome, thanks to Steve and Niels. The solution I used was [self.navigationController popViewControllerAnimated:YES]; easy ;) – oberbaum Jan 21 '10 at 08:54
  • this works great except viewcontrollers added on uitabbar. any clues?? – virata Jan 05 '12 at 11:40
  • 1
    I added `[self.navigationController popViewControllerAnimated:YES];` but it does nothing, however there is an alert on taping that button which is working. – mohsin.mr Jul 16 '13 at 11:23
  • Hmm, I get a blank screen, an unresponsive app and the following in the console: "Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted." – Kurt Jul 12 '14 at 08:35
24

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.

Kevin Elliott
  • 2,630
  • 3
  • 23
  • 21
23

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)
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
7

You should call

popViewControllerAnimated:

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

Niels Castle
  • 8,039
  • 35
  • 56
6
[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];
stema
  • 90,351
  • 20
  • 107
  • 135
Nishant Mahajan
  • 264
  • 4
  • 9