Why does the Master-Detail Application template crashes on iPhone 4 running iOS 7.1.2?

107 Views Asked by At

When I run the original (Objective-C) Master-Detail Application template using Xcode 7.3.1 on my iPhone 4 with the latest iOS (7.1.2) possible on that device, it crashes. I probably can't change the Base SDK which is set to iOS 9.3, but I'm using right Architecture and Deployment Target. Why does it crash?

Crashing code:

- (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];
    /**CRASH**/navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
    splitViewController.delegate = self;
    return YES;
}

Debugger says:

2016-08-04 21:14:26.240 Master-Detail-Application-2[766:60b] -[MasterViewController topViewController]: unrecognized selector sent to instance 0x16d61c20
2016-08-04 21:14:26.246 Master-Detail-Application-2[766:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MasterViewController topViewController]: unrecognized selector sent to instance 0x16d61c20'
*** First throw call stack:
(0x30dbef83 0x3b56fccf 0x30dc2917 0x30dc1203 0x30d10768 0x396a7 0x3364b587 0x3364aedb 0x3364556b 0x335e16e9 0x335e0851 0x33644ca9 0x35bf9aed 0x35bf96d7 0x30d89a67 0x30d89a03 0x30d881d7 0x30cf2ebf 0x30cf2ca3 0x33643ed1 0x3363f14d 0x3a945 0x3ba7cab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
1

There are 1 best solutions below

1
Evgeny Karkan On

The problem exists here:

UINavigationController *navigationController = [splitViewController.viewControllers lastObject];

See, you are assigning to navigationController the lastObject from view controllers stack. It can be not an instance of UINavigationController.

Sending a message that can not be handled by receiver - causes exception and runtime crash. Try to debug - of what class your navigationController is, and if it is not of UINavigationController type - fix that to be able to receive topViewControllermessage.