0

There are many answers but they don't answer the question. For example, I put the following code in:

    override func viewWillDisappear(animated: Bool) {
    if (self.navigationController!.viewControllers.indexOf(self)==nil) {
        print("back button pressed\n")
    }
    super.viewWillDisappear(animated)
}

But 'back button pressed' gets printed to the console even when the back button has not been pressed. The scene has buttons that return to the previous scene using unwind segues, and these cause 'back button pressed' to be printed. I need to execute code only if the back button has been pressed.

Edit for Muneeba:

import UIKit

class NewViewController: UIViewController, UINavigationBarDelegate {


func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem) {
    performSegueWithIdentifier("returnToStepOne", sender: self)
    delegate.backFromNewViewController()
}
override func viewDidLoad() {
    super.viewDidLoad()
    // self.navigationController.navigationBar.delegate = self;
    navigationController?.navigationBar.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}
Questioner
  • 2,451
  • 4
  • 29
  • 50
  • 1
    best solution is to hide default `backButtonItem` in navigation bar and add custom `ButtonItem` and assign action to it which navigates back. – Dipen Panchasara Dec 11 '15 at 12:18
  • @DipenPanchasara Is there any other way? The back button displayed is fine. I don't want to recreate it. – Questioner Dec 11 '15 at 12:27
  • the way i suggested is best way, otherwise you need to check where you are navigating, put condition for each navigation. – Dipen Panchasara Dec 11 '15 at 12:28
  • You can override the back button with your own back button and then call a function that executes the backing animation and whatever you'd like. – Arbitur Dec 11 '15 at 12:34
  • http://stackoverflow.com/questions/18824186/trying-to-handle-back-navigation-button-action-in-ios/18824282#18824282 – Kumar KL Dec 11 '15 at 12:38

1 Answers1

1

Try with following UINavigationBarDelegate.

 func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem)
Muneeba
  • 1,756
  • 10
  • 11
  • Is this supposed to happen when the back button is pressed? Because I put a print statement in this function's body and it never got printed when I tapped the back button. – Questioner Dec 11 '15 at 15:20
  • Have you set the delegate of UINavigationBar to the controller where you have implement that method? – Muneeba Dec 14 '15 at 04:14
  • I don't understand. How do I do that? – Questioner Dec 14 '15 at 11:44
  • In your second view Controller, the one that has back button , write following line in your view did load and implement the method as mentioned in above answer. self.navigationController.navigationBar.delegate = self; – Muneeba Dec 14 '15 at 11:46
  • 1
    First it said it wasn't unwrapped and I added a !. Then it said can't assign a value of type ViewController to UINavigationBarDelegate. So I added UINavigationBarDelegate to the class inheritance list. It compiled but gave this error: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.' – Questioner Dec 14 '15 at 12:00
  • I edited my question to include the code. I changed it to navigationController?.navigationBar.delegate = self and this stopped the initial crash. The problem is, once the unwind segue happens the previous view controller starts this view controller again and then the same crash happens. – Questioner Dec 14 '15 at 12:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97855/discussion-between-muneeba-and-questioner). – Muneeba Dec 14 '15 at 12:45
  • I gave up and just had it hit the same delegate function no matter which button is pressed in the controller. It's not ideal but I cannot waste any more time on this. – Questioner Dec 17 '15 at 09:33