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)
}
}
}
Related
I am trying to get the 3d touch quick action to work, I have this code below.
I do get a print saying it was pressed but it doesn't go to the view controller I want.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "LiveFeed" {
print("Tony: We got to live view")
guard let navVC = window?.rootViewController as? UINavigationController else { return }
let storyBoard: UIStoryboard = UIStoryboard(name: "LiveFeed", bundle: nil)
if #available(iOS 11.0, *) {
let composeViewController = storyBoard.instantiateViewController(withIdentifier: "LiveFeedMain") as! LiveFeedMain
navVC.pushViewController(composeViewController, animated: false)
} else {
// Fallback on earlier versions
}
}
}
I have this in the appDelegate
I implemented 3D touch before making changes to my UI, which all i did was embedded my viewControllers into navigation controller. After those changes, my quick add 3D shortcut crashes the app every time i try to launch it using 3D touch. If I launch the app regularly, the app works just fine.
Here is my main.storyboard
Here is my appDelegate 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! ViewController
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(addEventVC, animated: false, completion: { () -> Void in completionHandler(true)})
}
}
I have a 3D Force Touch Action that I set up in my Info.plist. Now I want that when the 3D Action is running it presents a view controller that I gave a Storyboard ID and I need to do this in the AppDelegate.
I used the performActionForShortCutItem function.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if let tabVC = self.window?.rootViewController as? UITabBarController {
if shortcutItem.type == "Hausaufgaben" {
tabVC.selectedIndex = 1
tabVC.performSegue(withIdentifier: "gotoHausaufgaben", sender: nil)
}
}
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "Hausaufgaben" {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "YOUR PAGE Identifier")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
I got a 3d touch function in my app delegate for the shortcutitems and it has to instantiate a viewcontroller when 3d touch item clicked from the iphone.
Here is the code:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.kevinvugts.addStuf" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let exerciseVC = sb.instantiateViewController(withIdentifier: "exerciseVC") as! exerciseVC
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(exerciseVC, animated: false, completion: { () -> Void in
completionHandler(true)
})
}
}
This code results in this warning/error:
Warning: Attempt to present <ActiveRest.exerciseVC: 0x131d39320> on <ActiveRest.ViewController: 0x131d0e470> whose view is not in the window hierarchy!
Has this anything to do with delay?
Thanks for any help!
Kind Regards.
view is not in the window hierarchy that means your view of your viewcontroller is not loaded in memory still. so you are unable to present viewcontroller on it. you should have to present viewcontroller from viewDidAppear of your viewcontroller (I mean from your rootviewController).
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.kevinvugts.addStuf" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let exerciseVC = sb.instantiateViewController(withIdentifier: "exerciseVC") as! exerciseVC
window?.rootViewController?.addChildViewController(exerciseVC)
}
}
I made this change and everything works fine.
I have my TabBarController as my initial view (storyboard identity Main) (Class UITabBarController)
Then a NavigationController (storyboard id = ViewController) (Class UINavigationController).
Then my ViewController (storyboard id = View) (Class ViewController)
Then my Details (storyboard id = StoryDetails) (Class Details)
I would like to navigate from AppDelegate.swift to the Details.swift uiviewcontroller.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let tabViewController = storyboard.instantiateViewControllerWithIdentifier("StoryDetails") as? UITabBarController {
window!.rootViewController!.presentViewController(tabViewController, animated: true, completion: nil)
}
But this is not working
Help would be really appreciated! (:
Also
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let detail = storyboard.instantiateViewControllerWithIdentifier("StoryDetails")
self.window?.rootViewController!.presentViewController(detail, animated: true, completion: nil)
This code brings up the view, but there is no navigation controller or tab bar controller