【问题标题】:Why does the Master-Detail Application template crashes on iPhone 4 running iOS 7.1.2?为什么主从应用程序模板在运行 iOS 7.1.2 的 iPhone 4 上崩溃?
【发布时间】:2016-12-11 03:55:51
【问题描述】:

当我在我的 iPhone 4 上使用 Xcode 7.3.1 运行原始 (Objective-C) 主从应用程序模板时,该设备上可能有最新的 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)

【问题讨论】:

  • topViewControllerUINavigationController 的方法,而不是UIViewController
  • @maddy 这是我从代码中看到的,没错。
  • 但错误表明您正在尝试在您的MasterViewController 上调用topViewController,而不是UINavigationController
  • @rmaddy 好点,但我标记了崩溃点。该模板适用于模拟器,我相信 iOS 9.3。

标签: objective-c ios7 xcode7 iphone-4


【解决方案1】:

问题出在这里:

UINavigationController *navigationController = [splitViewController.viewControllers lastObject];

你看,你正在从视图控制器堆栈中分配给navigationController lastObject。它不能是UINavigationController 的实例。

发送接收方无法处理的消息 - 导致异常和运行时崩溃。 尝试调试 - 你的 navigationController 属于什么类,如果不是 UINavigationController 类型 - 修复它以便能够接收 topViewControllermessage。

【讨论】:

  • 这是我提到的标准模板,所以它是Apple Inc.提供的代码(/**CRASH**/注释除外),它可以在模拟器中运行。我没有在其他任何地方更改或添加任何内容。就我而言,它开箱即用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2014-09-27
  • 2012-05-08
相关资源
最近更新 更多