3D touch quick actions preview view controller 只有一次

3D touch quick actions preview view controller only one time

我有以下问题。当我 运行 我的应用程序在我的 phone 上时,3d 触摸图标和 select 快速操作它会启动呈现正确视图控制器的应用程序,但是当我将应用程序置于后台并尝试调用快速操作它只是打开我离开它的应用程序。所以为了让它工作,我每次都必须终止我的应用程序。 这是我的代码:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    if shortcutItem.type == "com.traning.Search" {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let searchVC = sb.instantiateViewControllerWithIdentifier("searchVC") as! UINavigationController

        let root = UIApplication.sharedApplication().keyWindow?.rootViewController
        root?.presentViewController(searchVC, animated: false, completion: { () -> Void in
            completionHandler(true)
        })

    }
}

提前致谢。

我猜您正试图从一个不可见的视图控制器中呈现一个视图控制器。您可以使用如下扩展名:

    extension UIViewController {
    func topMostViewController() -> UIViewController {
        if self.presentedViewController == nil {
            return self
        }
        if let navigation = self.presentedViewController as? UINavigationController {
            return navigation.visibleViewController.topMostViewController()
        }
        if let tab = self.presentedViewController as? UITabBarController {
            if let selectedTab = tab.selectedViewController {
                return selectedTab.topMostViewController()
            }
            return tab.topMostViewController()
        }
        return self.presentedViewController!.topMostViewController()
    }
}

extension UIApplication {
    func topMostViewController() -> UIViewController? {
        return self.keyWindow?.rootViewController?.topMostViewController()
    }
}

您可以将这两个都放在您的应用程序 delegate.swift 中,在您的应用程序委托 class 上方,以获取当前可见的视图控制器。 然后将搜索视图控制器显示在那。例如:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    if shortcutItem.type == "com.traning.Search" {

        let sb = UIStoryboard(name: "Main", bundle: nil)

        let searchVC = sb.instantiateViewControllerWithIdentifier("searchVC") as! UINavigationController

        let topViewController = UIApplication.sharedApplication.topMostViewController()
        topViewController.presentViewController(searchVC, animated: false, completion: { () -> Void in
            completionHandler(true)
        })

    }
}