2017-05-02 31 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,只是添加了一些代码来回答。 –