问题
我想创建一个快速动作(3D触摸),允许用户直接进入游戏的统计部分。我的解决方案是基于这教程中的代码,并让它使用这段代码。我添加了一些额外的代码,因为我想使用快速操作来做一些与教程不同的事情。这是一个函数,负责从初始视图到统计数据视图执行一个segue。它在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
}
从以下委托函数调用此函数:
来自AppDelegate.swift
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
completionHandler(handleQuickAction(shortcutItem: shortcutItem))
}
如果应用程序已经关闭(从应用程序开关/多任务处理视图),并且我点击快速操作,这个设置将完美地工作。但是,如果我回家时没有完全关闭应用程序,并且尝试这样做,那么它不会转到统计数据视图,而是会转到游戏视图。
我已经尝试过很多次了,它偶尔会崩溃一次,下面的消息如下:
com.apple.CoreMotion.MotionThread (11):EXC_BAD_ACCESS (code=1,address=0x48307beb8)
我怎样才能改变它,使它按需要工作。
我试过什么
发布于 2017-08-16 22:50:48
试试像这样的东西
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)
}
}
}
}
https://stackoverflow.com/questions/45727803
复制相似问题