2016-04-30 82 views
0

我遇到了3D Touch的一些問題。它完美的工作,除非應用程序首次推出。這裏是我的AppDelegate下面的代碼:3D觸摸快速操作應用程序第一次啓動時不起作用

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)) 

} 

這裏在RAIntroductionViewControllerviewDidLoad方法的代碼:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.aboutMeTap), name: "about", object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.educationTap), name: "education", object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.projectsTap), name: "projects", object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.whyPressed(_:)), name: "why", object: nil) 

那些觀察家稱之爲推導航控制器的方法。

雖然一切正常,但導航控制器不會在應用程序第一次啓動時被推送到正確的頁面。它只是轉到根視圖控制器。

回答

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 
} 

希望這有助於:)

+0

謝謝你的答覆。我嘗試了這一點,但我得到一個錯誤:意外地發現零,同時解開一個可選的值。我假設'shortcutItem'變量爲零。有任何想法嗎? –

+0

很高興聽到您的反饋,代碼僅在'launchOptions'不是'nil'且'UIApplicationLaunchOptionsShortcutItemKey'有值時才起作用。我更新了代碼,只有在應用程序從3D Touch快捷方式啓動時,纔會執行'if'語句的主體。你介意嘗試新的代碼,看看它是否工作?謝謝! –

+0

感謝您的更新。我試過這個代碼,並沒有發生任何事情。沒有錯誤或崩潰,但沒有進入正確的頁面。爲了澄清,我正在使用NSNotificationCenter添加一個觀察者來執行此操作。 –

相關問題