Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 3D触控快速动作预览视图控制器仅一次_Ios_Swift_3dtouch_Quickaction - Fatal编程技术网

Ios 3D触控快速动作预览视图控制器仅一次

Ios 3D触控快速动作预览视图控制器仅一次,ios,swift,3dtouch,quickaction,Ios,Swift,3dtouch,Quickaction,我有以下问题。当我在手机上运行我的应用程序时,3d触摸图标并选择快速操作,它会启动显示正确视图控制器的应用程序,但当我将应用程序置于后台并尝试调用快速操作时,它只会在我离开应用程序的地方打开应用程序。所以为了让它正常工作,我必须每次都杀掉我的应用程序。 这是我的密码: func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem,

我有以下问题。当我在手机上运行我的应用程序时,3d触摸图标并选择快速操作,它会启动显示正确视图控制器的应用程序,但当我将应用程序置于后台并尝试调用快速操作时,它只会在我离开应用程序的地方打开应用程序。所以为了让它正常工作,我必须每次都杀掉我的应用程序。 这是我的密码:

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()
    }
}
您可以将这两个属性都放在app delegate.swift中,位于app delegate类的上方,以获取当前可见的视图控制器。然后在上面显示搜索视图控制器。例如:

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)
        })

    }
}

它是否调用performActionForShortcutItem?root=nil吗?它必须在哪里调用performationforshortcutitem?我的意思是尝试在performationforshortcutitem中打印一些内容以确保调用它。也许打印(root)这就是为什么你可以检查performationforshortcutitem是否被调用,并检查以确保root不是nil。问题是,在我启动应用程序后,它只调用一次方法,然后如果我将它放在后台并尝试调用该方法,则不会发生任何情况。你知道它没有调用它吗?或者它可能会被呼叫但什么也不做?按照我的建议做将有助于我们确定它是什么。你这样做吗?结果是什么?谢谢,但我仍然不明白该函数放在哪里以及调用它的位置。抱歉,我遗漏了一些内容。这行出现错误:extension UIViewController{声明仅在文件范围内有效您是否像我添加的屏幕截图中那样复制了它?是的,我将其放在@ApplicationMain之后。但即使在我实现了您所示的扩展之后,func应用程序也只是在启动后第一次工作。当它进入后台时,它就不再工作了。