1

I have two navigation Controllers for my views. I need to perform some operations when the user presses the back button to go to the previous view. Normally I would do this using the prepareForSague() function and using the sague Identifier.

What's the identifier (or how do I get it) of the back button that comes with navigation controller so i can use it in my prepareForSague function. Or is it done in other ways?

Bahram Ghebray
  • 107
  • 1
  • 11

5 Answers5

2

Please follow the steps:

  1. I think you have to first create custom button
  2. Assign it as back button of Navigation controller.
  3. Add target selector to that button and usepopToViewController: method.

Or

  1. Do connection with back button
  2. Add action to it
  3. write your back button logic in that method.
Ashok Londhe
  • 1,491
  • 11
  • 29
0

Just place your code inside viewWillDissapear

    override func viewWillDisappear(animated: Bool)

That is, don't try to change the behavior of the back button, just write whatever code you need to be executed inside viewWillDissapear

Diego Freniche
  • 5,225
  • 3
  • 32
  • 45
  • thanks I tried this. It works fine except, I also have another 'Cancel' Item on the right side. That is suppose to take the user back without saving data. So how do I make sure that, the Cancel button is not the source? – Bahram Ghebray Apr 07 '15 at 06:15
  • Just use a boolean iVar. When you press cancel you should launch some method. In that method, set the boolean cancelActivated. Now you can check in viewWillDissapear – Diego Freniche Apr 07 '15 at 07:12
  • This is also called when you application is sent to background in this screen. Or a call comes. Obviously, that's not the perfect solution. – rordulu Apr 07 '15 at 07:29
0

you have to use "viewWillDissapear" method for this, if you wanna use default back button.

But you may have to be careful here coz you only need this to work when really going back by back button, so every other actin - which calls "viewWillDissapear" method, you may need to make a flag to know its not the back action.

rjeprasad
  • 43
  • 5
0

One way of doing this would be to use the ViewWillDisappear() function to detect when the navigation back button is pressed. Found the answer here.

Community
  • 1
  • 1
arled
  • 2,577
  • 6
  • 27
  • 49
-1

your class can implement the UINavigationControllerDelegate protocol and implement this method:

- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
IgnazioC
  • 4,554
  • 4
  • 33
  • 46