我会尽力让自己尽可能清楚,但英语不是我的母语,所以对于给您带来的任何不便,我深表歉意。
我一直在关注这个http://www.appcoda.com/core-spotlight-framework/在聚光灯搜索上显示我的一些应用程序功能,但我陷入了显示每个功能的 View Controller 的部分。主要(我认为)因为我的应用程序是一个选项卡式应用程序。这是我的 Storyboard的屏幕截图:
因此,当尝试使用此方法时:
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
let viewController = (window?.rootViewController as! UINavigationController).viewControllers[0] as! consultaCorralones
viewController.restoreUserActivityState(userActivity)
return true
}
(consultaCorralones 是我希望显示的 View Controller )
当我运行它时,我收到错误:
Could not cast value of type 'UITabBarController' (0x11041d5d8) to 'UINavigationController' (0x11041d588).
我有限的 Swift 知识告诉我将 UINavigationController
更改为 UITabBarController
,但这样做我得到:
Could not cast value of type 'UINavigationController' (0x1117b8588) to 'Parcial2.consultaCorralones' (0x10fb83200).
我也尝试过关注Perform Segue from App Delegate swift但这没有用。
任何帮助将不胜感激。
最佳答案
UIApplication.sharedApplication().keyWindow?.rootViewController
返回一个 UITabBarController
。您需要从中获取 selectedViewController
。这将是一个 UINavigationController
实例。
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
let controller = UIApplication.sharedApplication().keyWindow?.rootViewController as! UITabBarController
let navController = controller.selectedViewController as? UINavigationController
let viewController = navController?.viewControllers[0] as! consultaCorralones
viewController.restoreUserActivityState(userActivity)
return true
}
希望有帮助。
关于ios - 聚光灯搜索后显示 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107450/