1

Is there anyway to implement the effect of back button on navigation bar?

For example: I have two view controllers, lets name then vc1(embeded in navigation controller) and vc2. On the navigation bar of vc2 I have a "done" button on the right side.

I want that when I navigate from vc1 to vc2, I will be able to go back to vc1 by pressing the "done" button on vc2. I want the effect of pressing the "done" button to be the same as pressing back button on navigation bar.

I tried using [self dismissViewControllerAnimated:YES completion:nil]; Using push segue won't work either.

Any help? Many Thanks!

JLT
  • 3,052
  • 9
  • 39
  • 86

3 Answers3

5

To go one viewcontroller back in Navigation View stack just use.

[self.navigationController popViewControllerAnimated: YES];

If you want to go to initial view controller from you started pushing, use

[self.navigationController popToRootViewControllerAnimated:YES];
Yogendra
  • 1,728
  • 16
  • 28
Reming Hsu
  • 2,215
  • 15
  • 18
3

if you want to pop animation then use popViewControlAnimation. and if you want to dismiss style animation then first present it not push and then dismiss it. because which vc you present only those vc can you dismiss

for pop animation

[self.navigationController popViewControllerAnimated: YES];

for dismiss animation

[self dismissViewControllerAnimated:YES completion:^{

    }];
Pravin Tate
  • 1,145
  • 8
  • 18
2

if you want to goback to the previous viewcontroller from where you have reached to vc2. you can use following code. This will bring you to last viewcontroller[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] animated:YES];

Mayank Barnwal
  • 123
  • 1
  • 12