3

In UINavigationController, is there a way to button if the current viewcontroller is being dismissed as a result of the user pressing the back button or if the view controller is being dismissed programmatically with popViewControllerAnimated?

Note: I am specifically trying to differentiate between the 2 events. This is not a duplicate of this question as I am trying to figure out which event was called, not when the view controller is being dismissed

To Clarify: I am trying to figure out whether the view is disappearing because

a) the back button was pressed or

b) popViewControllerAnimated was called

Community
  • 1
  • 1
Lneuner
  • 1,090
  • 1
  • 9
  • 19
  • Possible duplicate of [Detecting when the 'back' button is pressed on a navbar](http://stackoverflow.com/questions/8228411/detecting-when-the-back-button-is-pressed-on-a-navbar) – Teja Nandamuri Dec 22 '15 at 14:17
  • Hi. This is not a duplicate of this question as I am trying to figure out which event was called, not where the view controller is being dismissed – Lneuner Dec 22 '15 at 14:19
  • @Lneuner viewDidDisappear: and viewWillDisappear are the methods which get called when view is disappearing – Smile Dec 22 '15 at 14:34

1 Answers1

1

On viewWillDisappear method you can check values for isMovingFromParentViewController:

self.isMovingFromParentViewController()

which will return Bool, a Boolean value that indicates that the view controller is in the process of being removed from its parent.

Updated:

As replied, I think you will need to implement custom back button with its own method which can keep track of it.

self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = true; 

let backButton = UIBarButtonItem(title: "< Back", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Chalkduster", size: 20)!], forState: UIControlState.Normal)
navigationItem.backBarButtonItem = backButton
backButton.addTarget(self, action: "backButtonMethod",forControlEvents:UIControlEvents.TouchUpInside)
MobileGeek
  • 2,462
  • 1
  • 26
  • 46
  • Hi Arvind. This solution does not apply to my question – Lneuner Dec 22 '15 at 14:33
  • so you are not looking for exactly view controller removing(pop) from uinavigationcontroller stack. – MobileGeek Dec 22 '15 at 15:04
  • I am trying to figure out whether the view is disappearing because a) the back button was pressed or b) popViewControllerAnimated was called – Lneuner Dec 22 '15 at 15:06
  • This sound you will need to place your own custom back button to get back button track as in updated. – MobileGeek Dec 22 '15 at 15:15
  • Yeah I thought as much. I was just wondering if there was a simpler way – Lneuner Dec 22 '15 at 15:16
  • okay, better you create your own extension for back button. so that will works with complete app. – MobileGeek Dec 22 '15 at 15:18
  • I decided to take a different approach and to just set a flag in the method where I call popViewControllerAnimated. It's not the best/ most reusable solution, but I felt like making a category was a bit overkill for my use case – Lneuner Dec 23 '15 at 08:46