iOSAppGuy iOSAppGuy -4 years ago 108
iOS Question

When is an appropriate time to use presentedViewController?

Let's say I'm transitioning view controllers with segues. I have a textField on View Controller 1 and a label on the second view controller. When I'm segueing to the 2nd view controller I can easily set the label with the following code:

ViewController #2

-(void) viewWillAppear: (BOOL) animated
{
self.labelVC2.text = ((VC1 *)self.presentingViewController).textFieldVC1.text;
}


This makes perfect sense. The presentingViewController property is being used in this case. While trying to figure out when presentedViewController property would be used it seems logical that it would be used from the first view controller in the prepareForSegue method like this:

ViewController #1

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"SegueToNextVC"])
{
((VC2 *)self.presentedViewController).labelVC2.text = self.textFieldVC1.text;
}
}


However this doesn't seem to work and I'm assuming it's because in prepareForSegue a new instance of my 2nd view controller gets called the moment before the segue happens, thus losing the property I just set. If I'm am correct in this, when would be an appropriate time to use the presentedViewController property? Obviously I wouldn't use this property on my 2nd view controller because can I just access the class directly.

Answer Source

Take a look at the class reference for UIStoryboardSegue. It has a couple properties which I think would be very useful for you.

sourceViewController - "...view controller whose contents are displayed at the beginning of the segue", i.e. self

destinationViewController - "...view controller whose contents should be displayed at the end of the segue" which is your VC2 which your trying to access through self.presentedViewController

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
    if([[segue identifier] isEqualToString:@"SegueToNextVC"])
    {
       ((VC2 *)destinationViewController).labelVC2.text = self.textFieldVC1.text;
    }
 }

Back to your question though... when might you want to use presentedViewController? The answer is, whenever you don't already have a reference to the presentedViewController. Here's an example.

You have a bunch of classes which all have the same parent. SubViewController1, SubViewController2, SubViewController3 which are all sub-classes of MyViewController and you may or may not have one presented right now. So you want to check if it exists and tell it finish up what it's doing and leave. Happily, there is a method on MyViewController which will allow you to do just that.

if (self.presentedViewController) {
    MyViewController *subVC = (MyViewController *)self.presentedViewController;
    [subVC finishAndGoAway];
}

This is just one example of where you might use it. There are many other circumstances where it would be useful, but there are also lots of times when it's totally meaningless because you already have a reference to what's be presented so you don't need to use the default one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download