简体   繁体   中英

3D Touch Quick Action Won't work swift 3

I am trying to add 3D touch quick action in my app. I have two viewContollers embedded in navigationContoller. When the user press on the 3D action, it should take them to the add events viewController. But i am getting this error

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

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! 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.

Here is my main.storyboard

You shouldn't cast AddEventsViewController to UINavigationController , because it isn't UINavigationController , is just subclass of UIViewController . But as I see, your rootViewController is. So you need to cast it to UINavigationController , and then push your AddEventsViewController , instead present, and you will see 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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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