1

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!

Community
  • 1
  • 1
cohenadair
  • 2,072
  • 1
  • 22
  • 38
  • Why are the destination controllers different? Are you using different storyboards for the two different targets? – rdelmar Dec 02 '14 at 00:29
  • I'm not sure why they are different; I just assumed it was changed for iOS8. And no, I'm not, I'm using one storyboard. – cohenadair Dec 02 '14 at 00:31
  • As far as I know this shouldn't have changed between iOS 7 and 8. How are you testing this? Do you have 2 different apps, one for 7 and one for 8, or two targets, or what? – rdelmar Dec 02 '14 at 02:11
  • One app, an iPhone 6 simulator with iOS 8.1 and an iPhone 4s simulator with iOS 7.1. And I used a breakpoint to check the value of segue.destinationViewController. – cohenadair Dec 02 '14 at 02:32
  • I have exactly the same problem, and I didn't find documents about it. – xhg Feb 05 '15 at 20:00
  • I ended up doing what Andy said (although my app is now iOS8+ only). Check the class of the segue and use that VC or viewControllers objectAtIndex:0] if needed. – cohenadair Feb 06 '15 at 03:08

1 Answers1

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.

Community
  • 1
  • 1
cohenadair
  • 2,072
  • 1
  • 22
  • 38