IOS Master-Detail app template from xCode 6 does not start on iOS 7

I am trying to develop an iOS application for master parts (iPad only) from the xCode 6 template. It works fine with iOS 8, but running it on iOS 7.0 or 7.1 causes a runtime crash when I 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 error:

  • Open xCode 6
  • File> New> Project
  • Select “Application Master Data” below “iOS Application”
  • Change the project goal to 7.0
  • Run on an emulator or device

I have researched and it seems that object types are different on iOS 7 and iOS 8:

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

Why is this behavior?

+6
source share
4 answers

try this replacement:

 if ([splitViewController respondsToSelector:@selector(displayModeButtonItem)]){ navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem; } 
+3
source

Put this under prepareForSegue: to provide backward compatibility.

 DetailViewController *controller; if ([[segue destinationViewController] isKindOfClass:[UINavigationController class]]) { controller = (DetailViewController *)[[segue destinationViewController] topViewController]; } else { controller = (DetailViewController *)[segue destinationViewController]; } [controller setDetailItem:object]; 
+3
source

The engineer seems to have forgotten to check the backward compatibility of his template. Using the storyboard from the master-details xCode 5.1.1 template resolved the issue. For those of you who came 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

+1
source

displayModeButtonItem only works on iOS 8 or later.

+1
source

Source: https://habr.com/ru/post/977818/


All Articles