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

标签 ios swift ios11

问题

我想创建一个快速操作(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/

相关文章:

ios - Iphone - 使用 Swift 在 GIF 上叠加文本

iOS swift 标签栏的奇怪行为

ios - 如何在iOS中从didReceiveScriptMessage访问WKWebView?

android - 在 Unity 中影响移动设备 FPS 的因素

ios - 如何对齐 SKPhysicsBody 和 SKShapeNode?

ios - 通过点击 UITextField 判断是否有打开键盘的条件

ios - 插入行时单元格格式不正确

ios - 如何记录在 Xcode 中显示为运行时问题的警告?

ios - PDFKit 的 PDFDocument init(url : URL) does not work with HTTPS

ios - iOS 11 的商店服务崩溃