简体   繁体   English

3D Touch快速动作无法快速执行3

[英]3D Touch Quick Action Won't work swift 3

I am trying to add 3D touch quick action in my app. 我正在尝试在我的应用中添加3D触摸快速动作。 I have two viewContollers embedded in navigationContoller. 我在NavigationContoller中嵌入了两个viewContollers。 When the user press on the 3D action, it should take them to the add events viewController. 当用户按下3D动作时,应将其带到添加事件viewController中。 But i am getting this error 但是我收到这个错误

could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360). 无法将类型'Morning_Star_2.AddEventsViewController'(0x1000a2260)的值强制转换为'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). 2017-05-02 02:51:36.220324-0400 Morning Star 2 [3731:895126]无法将类型'Morning_Star_2.AddEventsViewController'(0x1000a2260)的值强制转换为'UINavigationController'(0x1a79cd360)。

Here is my code 这是我的代码

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

I tried changing as! 我尝试更改为! UINavigationController to as! UINavigationController改为! AddEventsViewController. AddEventsViewController。 It works, but then it won't show the navigation bar on the top on my add viewController as it normally would if you open the app normally. 它可以工作,但是然后不会像我正常打开应用程序时那样在我的添加viewController的顶部显示导航栏。

Here is my main.storyboard 这是我的main.storyboard

You shouldn't cast AddEventsViewController to UINavigationController , because it isn't UINavigationController , is just subclass of UIViewController . 您不应该将AddEventsViewControllerUINavigationController ,因为它不是UINavigationController ,而只是UIViewController子类。 But as I see, your rootViewController is. 但正如我所见,您的rootViewController是。 So you need to cast it to UINavigationController , and then push your AddEventsViewController , instead present, and you will see NavigationBar 因此,您需要将其UINavigationControllerUINavigationController ,然后推送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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM