Is it possible to detect which 'UIViewController' has been popped in the root view controller? I have 3 view controllers, A->B->C. Both B and C have popping actions. Is it possible to detect whether it was B or C was popped in A?
Asked
Active
Viewed 57 times
0
-
You can set it by simple `NSUserDefaults` flag, that would be easiest way to do things like this. – Viral Savaj Apr 20 '15 at 10:41
-
You can use `NSNotificationCenter` OR `NSUserDefaults` for this. – Yogesh Suthar Apr 20 '15 at 10:41
-
I would set A controller as a **UINavigationControllerDelegate** and implement: *- navigationController:willShowViewController:animated:* or *- navigationController:didShowViewController:animated:* – DanielS Apr 20 '15 at 10:44
-
2@DanielS Assuming you want to look at the navigation stack in those methods, it won't work. When `navigationController:willShowViewController:animated:` gets called, the navigation stack (navigationController.viewControllers) is already modified. (In the case of popToRoot, only the rootViewController will be in it). – Lord Zsolt Apr 20 '15 at 10:49
-
@LordZsolt yes, you're right. My mistake. – DanielS Apr 20 '15 at 12:07
1 Answers
1
I much rather prefer using delegate for this over something likfe NSNotificationCenter
or NSUserDefaults
.
You define a protocol, which has a method like this:
- (void)willPopToRootFromViewController(UIViewController *)fromViewController;
You implement this method in A, and the other view controllers call this method before popping.
You can find a complete example on how to implement a delegate here.

Community
- 1
- 1

Lord Zsolt
- 6,492
- 9
- 46
- 76
-
-
What do you mean? `fromViewController` would be the view controller you're popping from (aka `B` or `C`) and self will be `A` – Lord Zsolt Apr 20 '15 at 11:05
-
-
That's what the delegate is for. You would have a **weak** property to a class which implements that protocol (in that case your A object). – Lord Zsolt Apr 20 '15 at 11:15