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

标签 ios swift 3dtouch quickaction

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

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

    }
}

提前致谢。

最佳答案

我猜你正试图从一个不可见的 View Controller 中呈现一个 View Controller 。您可以使用如下扩展:

    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)类之上的 app delegate.swift 中,以获取当前可见的 View Controller 。 enter image description here然后在上面显示搜索 View Controller 。例如:

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

    }
}

关于ios - 3D touch quick actions preview view controller 只有一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34049946/

相关文章:

swift - 使用 Swift 将 slider 连接到 OS X Cocoa 中的单独图形 View

arrays - Swift - 排序和分离字典数组

Swift:在应用程序退出时清除 .sharedApplication().shortcutItems

swift - UIViewControllerPreviewing peek 不显示我的预览

objective-c - 随机数选择器总是只在应用程序首次启动时选择相同的数字

javascript - 使移动网络应用程序能够在用户移动默认浏览器中打开链接,而不是在应用程序内部打开

ios - 单击允许后未调用 didChangeAuthorization Status

ios - 使用没有文件扩展名的 SDWebImage 加载图像

swift - RxSwift+Alamofire 自定义映射器错误处理

ios - 使用 3D Touch 快速操作时将 $(PRODUCT_BUNDLE_IDENTIFIER) 传递给参数