3D Touch 操作无效 (swift 3)

3D Touch actions not working (swift 3)

所以,我已经成功地把所有东西都连接好了。当按下快捷键操作时,我会在控制台中得到正确的打印结果。我从这里去哪里?我想不通...

这是我在 App Delegate 中的代码

    var quicklaunch: Bool!
    var viewName: String?

   func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    viewName = shortcutItem.type
    quicklaunch = true
    completionHandler(quicklaunch)
}

func applicationDidBecomeActive(application: UIApplication) {
    if quicklaunch == true {
        quicklaunch = false
        if viewName == "Messages" {
            let rootViewController = self.window?.rootViewController
            let popUpController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FriendsVC")
            rootViewController?.present(popUpController, animated: true, completion: nil)
            print("Worked")
        }
    }  
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    if quicklaunch == false {
        quicklaunch = true
        if viewName == "Messages" {
            let rootViewController = self.window?.rootViewController
            let popUpController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FriendsVC")
            rootViewController?.present(popUpController, animated: true, completion: nil)
            print("Worked")
        }
    }

}

如何让它弹出到正确的视图控制器?

您可以获得 rootViewController,然后以模态方式呈现所需的 viewController(或者如果您的 rootViewControllerUINavigationController,则将其推送到导航堆栈)。类似于

let rootViewController = self.window?.rootViewController
let popUpController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "???") as! ???
rootViewController.present(popUpController, animated: true, completion: nil)

???替换为相应的值

编辑

在您的 AppDelegate 中创建一个名为 quickLaunch 的布尔变量和一个字符串变量 viewName。然后更新下面两个方法

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    viewName = shortcutItem.type
    quickLaunch = true
    completionHandler(quickLaunch)
}

func applicationDidBecomeActive(application: UIApplication) {
    if quickLaunch == true {
        quickLaunch = false
        //PRESENT YOUR VIEW MODALLY HERE
    }  
}