Reference to the segue source view controller
Asked Answered
S

3

9

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?

Strangle answered 30/5, 2013 at 22:18 Comment(1)
Do you mean in the destination controller? You're last sentence is not clear.Numerical
N
7

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;
Numerical answered 30/5, 2013 at 22:41 Comment(0)
H
5

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.

Herby answered 30/5, 2013 at 22:20 Comment(3)
This will only work if the segue is modal, it won't work for a push.Numerical
It sounds like he's talking about a modal segue, but yeah good call.Herby
I get my rootViewController by calling this method, so it seems the logic is a bit broken.Mammillate
B
0

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.

Buddhology answered 1/12, 2015 at 23:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.