Reference to the segue source view controller

2019-04-22 05:38发布

问题:

Within my viewDidLoad I would like some custom code based upon the previous controller.

How can I access the segue source controller or the previous segue identifier in the destination controller's viewDidLoad to handle this?

回答1:

There is no way to get a reference to the segue that created you. You could create a property (sourceVC in my example) in the destination controller, and assign self to this property in the prepareForSegue method (in the source view controller):

[(DestinationVCClass *)segue.destinationViewController sourceVC] = self;


回答2:

You can just use [self presentingViewController] and you'll be able to access the VC that issued the segue. I usually like to couple it with isMemberOfClass: for a situation like this.



回答3:

You do the following in the unwinding segue method in the destination

self.source = (UIStoryboardSegue *)segue.sourceViewController;

Define source as a UIStoryboardSegue in the destination. The above line will give the source or the previous segue.