gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:52:12 28 4
gpt4 key购买 nike

我有以下问题。当我在我的手机上运行我的应用程序时,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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com