我有一个简单的 UISplitViewController
扩展:
extension UISplitViewController {
var masterViewController: UIViewController? {
return (viewControllers.first as? UINavigationController)?.topViewController
}
}
在一些 UIViewController
中,我确实将模态转场连接到我的 UISplitViewController
。当执行 segue 时,我会像这样准备我的 destinationViewController
:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let searchResultsViewController = (segue.destinationViewController as? UISplitViewController)?.masterViewController as? SearchResultsViewController {
searchResultsViewController.mode = mode
}
}
由于错误,它不起作用:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'The content view controller argument must be the root of its associated view controller hierarchy.'
但是,当我删除 UISplitViewController
的扩展并准备 destinationViewController
时,如下所示:
if let searchResultsViewController = ((segue.destinationViewController as? UISplitViewController)?.viewControllers.first as? UINavigationController)?.topViewController as? SearchResultsViewController {
//prepare controller
}
它有效
What is the reason that it is not working in first case?
最佳答案
尝试将计算变量的名称从 masterViewController
更改为其他名称。您似乎正在覆盖 UISplitViewController
的内部属性。
这有效,并且在 UISplitViewController
的设置过程中不会被调用。
var masterVC: UIViewController? {
return (viewControllers.first as? UINavigationController)?.topViewController
}
这由 SDK 中的某些内容调用并导致崩溃。
var masterViewController: UIViewController? {
return (viewControllers.first as? UINavigationController)?.topViewController
}
关于ios - UISplitViewController 扩展给应用带来了麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38786133/