2017-05-02 84 views
0

我想在我的應用程序中添加3D觸摸快速操作。我有兩個viewContollers嵌入在navigationContoller中。當用戶按下3D動作時,它應該將它們帶到add事件viewController。但我得到這個錯誤3D觸摸快速操作不會工作迅速3

could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360). 2017-05-02 02:51:36.220324-0400 Morning Star 2[3731:895126] Could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360).

這裏是我的代碼

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { 
    if shortcutItem.type == "com.krp.addEvent" { 
     let sb = UIStoryboard(name: "Main", bundle: nil) 
     let addEventVC = sb.instantiateViewController(withIdentifier: "addEventVC") as! UINavigationController 
     self.window?.rootViewController?.present(addEventVC, animated: true, completion: nil) 
    } 
} 

我試圖改變的! UINavigationController爲! AddEventsViewController。它可以工作,但是如果您正常打開應用程序,它將不會像我通常在添加viewController時那樣在頂部顯示導航欄。

Here is my main.storyboard

+0

您能不能告訴你的故事板的圖片? –

+0

它在那裏@BhupatBheda。請點擊「這是我的main.storyboard」 –

回答

2

你不應該投AddEventsViewControllerUINavigationController,因爲它不是UINavigationController,只是UIViewController子類。但正如我所看到的,你的rootViewController是。所以,你需要將其轉換爲UINavigationController,然後把你的AddEventsViewController,而不是現在,你會看到NavigationBar

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { 
     if shortcutItem.type == "com.krp.addEvent" { 
     let sb = UIStoryboard(name: "Main", bundle: nil) 
     let addEventVC = sb.instantiateViewController(withIdentifier: "addEventVC") as! AddEventsViewController 
     if let navigationController = window?.rootViewController as? UINavigationController { 
      navigationController.pushViewController(addEventVC, animated: true) 
     } 
    } 
} 
+0

@blueren,是這樣的? 如果是這樣,我得到一個錯誤:self.window?.rootViewController?.push(addEventVC,animated:true,completion:nil) –

+0

我不太清楚你的意思。你能否通過代碼解釋它? –

+0

@KunjPatel,只是添加了一些代碼來回答。 –