在最近的项目中,我实现了聚光灯搜索,并且一切正常(我使用了UINavigationController)。现在,我将注意力集中在另一个项目上,该项目不包含UINavigationController,而仅包含两个UIViewController。同样在这种情况下,当在聚光灯搜索中按下特定项目时,除了重定向到详细视图控制器之外,其他一切都正常。我发现问题出在这种方法中,这是因为我没有使用UINavigationController。所以我的问题是,如何更改此代码使其仅与两个UIViewController一起使用?
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Swift.Void) -> Bool {
let navController = self.window!.rootViewController as! UINavigationController
if let actID = userActivity.userInfo!["kCSSearchableItemActivityIdentifier"] as? String {
let ricette = DataManager.shared.arra.filter { $0.id.uuidString == actID }
if let ricetta = ricette.first {
debugPrint("Abbiamo la ricetta grazie all'ID")
if let listController = navController.topViewController as? ListController {
if let posizione = DataManager.shared.arra.index(of: ricetta) {
listController.showDetailFromSpotlightSearch(posizione)
}
} else if let ricettaController = navController.visibleViewController as? RicettaController {
ricettaController.ricetta = ricetta
ricettaController.aggiornaInterfaccia()
}
} else { debugPrint("errore ID") }
} else { debugPrint("errore ID") }
return true
}
答案 0 :(得分:0)
您应该使用这两个扩展名来确定topViewController和visibleViewController:
extension UIApplication {
class func getVisibleViewController(_ rootViewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let presentedViewController = rootViewController?.presentedViewController {
return getVisibleViewController(presentedViewController)
}
return rootViewController
}}
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}}