3D应用首次启动时触摸快速操作无法正常工作

时间:2016-04-30 00:40:11

标签: ios swift 3dtouch

我在使用3D Touch时遇到了一些问题。除了首次启动应用程序外,它的工作方式非常完美。以下是我的AppDelegate中的以下代码:

 func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var quickActionHandled = false
    let type = shortcutItem.type.componentsSeparatedByString(".").last!
    if let shortcutType = Shortcut(rawValue: type) {
        switch shortcutType {
        case .aboutMe:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.aboutMeTap), name: "about", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("about", object: self)
            quickActionHandled = true
        case .education:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.educationTap), name: "education", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("education", object: self)
            quickActionHandled = true
        case .projects:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.projectsTap), name: "projects", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("projects", object: self)
            quickActionHandled = true
        case .why:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.whyPressed(_:)), name: "why", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("why", object: self)
            quickActionHandled = true
        }
    }
    return quickActionHandled
}

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    completionHandler(handleQuickAction(shortcutItem))

}

以下是viewDidLoad的{​​{1}}方法中的代码:

RAIntroductionViewController

那些观察者调用推动导航控制器的方法。

虽然一切都运行良好,但在首次启动应用程序时,导航控制器不会被推到正确的页面。它只是转到根视图控制器。

1 个答案:

答案 0 :(得分:3)

首次启动应用时,不会调用application:performActionForShortcutItem shortcutItem:completionHandler:

相反,您应该在application:didFinishLaunchingWithOptions:中处理此问题。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
    if let options = launchOptions,
       let shortcutItem = options[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
       // do the work here..
    }
    return true
}

希望这会有所帮助:)