Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In my viewDidAppear, how do I know when it's being unwound by a child?

Tags:

When my child performs an unwind segue, my controller's viewDidAppear gets called.

In this method (and this method alone, I need to know whether it was from an unwind or not)

Note: the child is unwinding to the very first view controller, so this is an intermediate view controller, not the true root.

like image 415
TIMEX Avatar asked Apr 14 '15 02:04

TIMEX


1 Answers

You should be able to use the following to detect in each controller if the exposure of the view controller was as a result of being pushed/presented, or as a result of being exposed as a result of pop/dismiss/unwind.

This may or may be enough for your needs.

- (void) viewDidAppear:(BOOL)animated{     [super viewDidAppear:animated];      // Handle controller being exposed from push/present or pop/dismiss     if (self.isMovingToParentViewController || self.isBeingPresented){         // Controller is being pushed on or presented.     }     else{         // Controller is being shown as result of pop/dismiss/unwind.     } } 

If you want to know that viewDidAppear was called because of an unwind segue as being different from a conventional pop/dismiss being called, then you need to add some code to detect that an unwind happened. To do this you could do the following:

For any intermediate controller you want to detect purely an unwind in, add a property of the form:

/** BOOL property which when TRUE indicates an unwind occured. */ @property BOOL unwindSeguePerformed; 

Then override the unwind segue method canPerformUnwindSegueAction:fromViewController:withSender: method as follows:

- (BOOL)canPerformUnwindSegueAction:(SEL)action                  fromViewController:(UIViewController *)fromViewController                          withSender:(id)sender{   // Set the flag indicating an unwind segue was requested and then return   // that we are not interested in performing the unwind action.   self.unwindSeguePerformed = TRUE;    // We are not interested in performing it, so return NO. The system will   // then continue to look backwards through the view controllers for the    // controller that will handle it.   return NO; } 

Now you have a flag to detect an unwind and a means to detect the unwind just before it happens. Then adjust the viewDidAppear method to include this flag.

- (void) viewDidAppear:(BOOL)animated{     [super viewDidAppear:animated];      // Handle controller being exposed from push/present or pop/dismiss     // or an unwind     if (self.isMovingToParentViewController || self.isBeingPresented){         // Controller is being pushed on or presented.         // Initialize the unwind segue tracking flag.         self.unwindSeguePerformed = FALSE;     }     else if (self.unwindSeguePerformed){         // Controller is being shown as a result of an unwind segue     }     else{         // Controller is being shown as result of pop/dismiss.     } } 

Hopefully this meets your requirement.

For docs on handling the unwind segue chain see: https://developer.apple.com/library/ios/technotes/tn2298/_index.html

like image 85
Rory McKinnel Avatar answered Nov 16 '22 05:11

Rory McKinnel