gpt4 book ai didi

swift - 如何让 3d Touch Quick Action 在我的应用程序中打开某个 View Controller ?

转载 作者:行者123 更新时间:2023-11-28 12:11:36 25 4
gpt4 key购买 nike

我有一个具有 3D Touch 快速操作的应用程序,我想让它在我的应用程序中启动某个 View Controller 。

有人可以告诉我在我的应用程序中启动某个 View Controller 的正确代码是什么吗?

在我的 AppDelegate.swift 中,我有这个……但它不起作用。

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
if shortcutItem.type == "Matt-Held.blackshirtMarket.viewproducts" {

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination as!
productViewController
}

我想在按下 3d 快速操作时“启动”的 View Controller 称为“productViewController”

提前致谢!

最佳答案

您想从 Storyboard 中实例化您的目标 VC,如下所示:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let targetVC = storyboard.instantiateViewController(withIdentifier :"productViewController") as! ProductViewController

然后您想决定如何处理它。一些常见的选项可能是 a) 以模态方式呈现,b) 将其推送到现有的 Nav Controller 上,或 c) 仅使其成为应用程序窗口的 rootVC。下面是你将如何做后者:

 // option B) push onto an existing nav controller
if let navC = window?.rootViewController as! UINavigationController? {
navC.pushViewController(targetVC, animated: false)
}

// option C) just make it the root VC
window?.rootViewController = targetVC

我认为其他方法在其他地方有详细记录。你想要什么取决于你的应用程序的导航结构(你没有包含在问题中)。

除此之外,请查看示例快捷方式应用程序。请记住,当使用快速操作从后台激活您的应用程序时,将调用此方法 performActionForShortcut。因此,您还需要处理通过快捷方式启动应用程序的问题。

关于swift - 如何让 3d Touch Quick Action 在我的应用程序中打开某个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48332017/

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