gpt4 book ai didi

ios - 当应用程序已打开时,Segue 不执行快速操作(Xcode 9)

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

问题

我想创建一个快速操作(3D Touch),允许用户直接进入游戏的统计部分。我的解决方案基于 this 中的代码教程并让它与此代码一起使用。我添加了一些额外的代码,因为我想使用快速操作来执行与教程中不同的操作。该函数负责执行从初始 View 到统计 View 的转换。它位于 AppDelegate.swift 中:

来自 AppDelegate.swift

func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {

var quickActionHandled = false
let type = shortcutItem.type.components(separatedBy: ".").last
if let shortcutType = Shortcut.init(rawValue: type!) {
switch shortcutType {
case .statistics:
quickActionHandled = true

// I use dispatchQueue to ensure that the segue occurs immediately
DispatchQueue.main.async {
self.window?.rootViewController?.performSegue(withIdentifier: "ARView_to_stats", sender: self.window?.rootViewController)
}

}
}

return quickActionHandled
}

此函数是从以下委托(delegate)函数调用的:

来自 AppDelegate.swift

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
completionHandler(handleQuickAction(shortcutItem: shortcutItem))
}

如果应用程序已关闭(从应用程序切换器/多任务 View )并且我点击快速操作,则此设置可以完美运行。但是,如果我没有完全关闭应用程序就回家并尝试执行相同的操作,它不会进入统计 View ,而是进入游戏 View 。

我已经尝试了很多次,但偶尔会崩溃并显示以下消息:

com.apple.CoreMotion.MotionThread (11):EXC_BAD_ACCESS(代码=1,地址=0x48307beb8)

我怎样才能改变它以使其按预期工作。

我尝试过的

  • 删除 DispatchQueue.main.async

最佳答案

尝试这样的事情

var isQuickLaunched = false
var quickLaunchOption: Shortcut!

func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
var quickActionHandled = false
let type = shortcutItem.type.components(separatedBy: ".").last
if let shortcutType = Shortcut.init(rawValue: type!) {
isQuickLaunched = true
quickLaunchOption = shortcutType
quickActionHandled = true
}

return quickActionHandled
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
completionHandler(handleQuickAction(shortcutItem: shortcutItem))
}

func applicationDidBecomeActive(application: UIApplication) {
if (isQuickLaunched == true) {
isQuickLaunched = false
switch quickLaunchOption {
case .statistics:
DispatchQueue.main.async {
self.window?.rootViewController?.performSegue(withIdentifier: "ARView_to_stats", sender: self.window?.rootViewController)
}
}
}
}

关于ios - 当应用程序已打开时,Segue 不执行快速操作(Xcode 9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45727803/

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