我正在尝试创建一个快速操作,该操作将导航到我的选项卡栏 (vc1) 中的第二个视图控制器,然后调用一个将另一个视图控制器 (vc2) 推送到该视图控制器上的函数。Vc1 有一个名为 handleShowPopUp 的函数,其中包含以下代码:
@objc func handleShowPopUp() {
self.navigationController?.pushViewController(self.popUpWindow, animated: true)
}
当它被 vc1 上的一个按钮调用时,这个函数工作得很好。但是,当我尝试从我的 SceneDelegate 调用它以响应快速操作时,它什么也不做,而且我发现当 handleShowPopUp 尝试推送 vc2 时,它返回 nil。我尝试将导航控制器显式设置为 vc1,但即便如此它仍然返回 nil。我怎样才能解决这个问题?这是我的 SceneDelegate 代码:
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
completionHandler(handleShortcutItem(shortcutItem: shortcutItem))
}
func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
var handle = false
guard let shortcutType = shortcutItem.type as String? else {return false}
switch(shortcutType){
case "{identifier}":
handle = true
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let vc1 = storyboard.instantiateViewController(identifier: "vc1") as? VC else {return false}
let tabVC = window?.rootViewController as! UITabBarController
tabVC.selectedIndex = 1
//move to vc1
vc1.handleShowPopUp()
//push vc2 (not working b/c navigationcontroller is nil)
break
default:
break
}
return handle
}