为什么Master-Detail Application模板在运行iOS 7.1.2的iPhone 4上崩溃?

时间:2016-08-04 20:57:15

标签: objective-c ios7 xcode7 iphone-4

当我在我的iPhone 4上使用Xcode 7.3.1运行原始(Objective-C)Master-Detail应用程序模板时,该设备上可能有最新的iOS(7.1.2),它会崩溃。我可能无法更改设置为iOS 9.3的Base SDK,但我使用了正确的架构和部署目标。为什么会崩溃?

崩溃代码:

- (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;
}

调试器说:

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 个答案:

答案 0 :(得分:0)

这里存在问题:

UINavigationController *navigationController = [splitViewController.viewControllers lastObject];

请参阅,您从视图控制器堆栈分配navigationController lastObject。它可能不是UINavigationController的实例。

发送无法由接收方处理的消息 - 导致异常和运行时崩溃。 尝试调试 - 您navigationController所属的类,如果不属于UINavigationController类型,请修复该类以便能够接收topViewController消息。