iOS7 and iOS8 segue.destinationViewController

2020-08-01 06:04发布

问题:

I'm trying to make my app work with both iOS7 and iOS8 and I ran into a problem in my prepareForSegue methods in my view controllers.

In iOS8 segue.destinationViewController is of class UINavigationController, so I use [[segue.desinationViewController viewControllers] objectAtIndex:0] which works fine, but in iOS7 segue.desinationViewController is of class CMAMyViewControllerClassName, which will obviously throw an error when I try to send a viewContollers message to it.

I found this solution which will work, but I was wondering if there's a better solution? Other than the latter post I haven't been able to find anything about it. If there's not a "proper" solution, I'll create a method that gets the correct view controller; I was just wondering how other people handled this situation.

Thanks!

回答1:

This issue is resolved by checking the class of the destination view controller before trying to access one of its properties:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DestController *vc = segue.destinationViewController;

    if ([dvc isKindOfClass:[DestController class]])
        dvc.propertyName = @"Property Value";
    else
        // do something else
}

Taken from a solution in this thread.