Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prepareForSegue and delegates

I have an application with two segues. In one of the segues, the current view controller becomes a delegate and the other does not.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"MoreOptions"]) {
        UINavigationController *navigationController = segue.destinationViewController;
        MoreOptionsViewController *controller = (MoreOptionsViewController *)navigationController.topViewController;
        controller.delegate = self;
    } else if ([segue.identifier isEqualToString:@"FullStoryView"]) {
        SingleStoryViewController *detailViewController = segue.destinationViewController;
        detailViewController.urlObject = sender;
    }
}

All of this is working fine, but I would like to try and understand the code better. What I don't understand is that I have to get a reference to the MoreOptionsViewController by grabbing it from navigationController.topViewController rather than simply getting it from segue.destinationViewController like I do in the second if condition. Is it because I'm setting the current view controller (self) as the delegate? Again, I'm not trying to solve a problem, just trying to get a better understanding of what's going on.

like image 299
Sonny Parlin Avatar asked Dec 20 '11 18:12

Sonny Parlin


2 Answers

Take a look at your storyboard and it should be evident why this is the case. You have embedded MoreOptionsViewController in a UINavigationController and connected a segue to the navigation controller, thus making it the destinationViewController. This is fairly common.

like image 69
Mark Adams Avatar answered Nov 15 '22 00:11

Mark Adams


The delegate is largely irrelevant in the context of your question.

Your first segue's destination is a navigation controller, which contains the view controller you are really interested in. Therefore to get to that view, you need to go through the navigation controller since that won't have any properties you are interested in setting.

Your second segue goes directly to a single view controller, so you can access it directly.

like image 43
jrturton Avatar answered Nov 15 '22 02:11

jrturton