iOS Master-Detail application template from xCode 6 doesn't run on iOS 7
Asked Answered
B

4

6

I'm trying to develop a master-detail iOS application (iPad only) from the xCode 6 template. It runs fine with iOS 8 but running it on iOS 7.0 or 7.1 produces a crash at run-time where I've commented:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];

    // this line throws a "[MasterViewController topViewController]: unrecognized selector sent to instance 0x796dde90"
    navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
    splitViewController.delegate = self;
    return YES;
}

To reproduce the bug :

  • Open xCode 6
  • File > New > Project
  • Choose "Master-Details Application" below "iOS application"
  • Change target of the project to 7.0
  • Run on emulator or device

I investigated and it seems that object types differs on iOS 7 and iOS 8:

  • On iOS8, self.window.rootViewController is a UISplitViewController
  • On iOS7, self.window.rootViewController is the first UINavigationController (left)

Why this behavior?

Bobbi answered 6/11, 2014 at 9:14 Comment(1)
Are you testing on iPhone or iPad?Valine
P
3

try this replacement:

if ([splitViewController respondsToSelector:@selector(displayModeButtonItem)]){
    navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
}
Perspiration answered 6/11, 2014 at 9:48 Comment(5)
It works but now I have only the "master" view. No more "detail" view.Bobbi
ah ok, so we need an else{} afterwards to deal with pre iOs8. I don't have a split view project unfortunately, sorry. Id need to look at a splitView template from xCode5Perspiration
I'm currently downloading xCode 5 to test it too.Bobbi
It works by replacing the storyboard from the xCode 6 template by the xCode 5 one... The SplitViewController displays differently now in my xCode 6 project, as in xCode 5.Bobbi
well if you take a good look at where it sets itself all up in the appDelegate you might find a way to use that ifRespondsToSelector: stuff in my answer to get the new stuff working where possible. There is a lot to learn from those templates, they change with each xCode update and I suspect they're prepared by a few different engineers because you can see a few different styles in them. Well done :)Perspiration
S
3

Put this under prepareForSegue: to ensure backwards compatibility.

DetailViewController *controller;
    if ([[segue destinationViewController] isKindOfClass:[UINavigationController class]]) {
        controller = (DetailViewController *)[[segue destinationViewController] topViewController];
    }
    else {
        controller = (DetailViewController *)[segue destinationViewController];
    }
[controller setDetailItem:object];
Swivet answered 28/11, 2014 at 9:37 Comment(0)
B
1

The engineer seems to have forgotten to check the backward compatibility of his template. Using the storyboard from the xCode 5.1.1 master-details template resolved the problem. For those of you coming from Google, you can download xCode 5.1.1 here : https://developer.apple.com/devcenter/download.action?path=/Developer_Tools/xcode_5.1.1/xcode_5.1.1.dmg

Bobbi answered 6/11, 2014 at 12:41 Comment(0)
S
1

displayModeButtonItem only work on iOS 8 or later.

Sopping answered 10/2, 2015 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.