Bahram Ghebray
Bahram Ghebray

Reputation: 117

How do I perform an operation when the back button is pressed

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?

Upvotes: 1

Views: 604

Answers (5)

Ashok Londhe
Ashok Londhe

Reputation: 1491

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.

Upvotes: 2

arled
arled

Reputation: 2690

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.

Upvotes: 0

rjeprasad
rjeprasad

Reputation: 43

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.

Upvotes: 0

Diego Freniche
Diego Freniche

Reputation: 5414

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

Upvotes: 0

Ignazio
Ignazio

Reputation: 4904

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

- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

Upvotes: -1

Related Questions