-1

I'm trying to go to any out of the 3 tab bar elements that I have in my storyboard depending on which quick action I select when 3D Touching on the app icon. I want to still be able to see the tab bar controller, which is where I've been struggling recently. Any help would be greatly appreciated.

Edit: I have got it to go to each separate Tab Bar Items.

I now need to activate the segue in each view, and here is the code that I'm using:

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    let myTabBar = self.window?.rootViewController as? UITabBarController
    let NavigationController = UINavigationController.self
    if shortcutItem.type == "com.LukeB.Quick-Actions-Test.Three" {
        myTabBar?.selectedIndex = 2
        //Need to go to the segue with identifier SegueThree
    }
    if shortcutItem.type == "com.LukeB.Quick-Actions-Test.Two" {
        myTabBar?.selectedIndex = 1
        //Need to go to the segue with identifier SegueThree

    }
    if shortcutItem.type == "com.LukeB.Quick-Actions-Test.One" {
        myTabBar?.selectedIndex = 0
        //Need to go to the segue with identifier SegueOne


    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Luke B
  • 288
  • 2
  • 5
  • 18

1 Answers1

2

The behavior you are looking for is explained in the documentation for UIApplicationDelegate's quick action handler here. To summarize, you need to check the launch options in your willFinishLaunching or didFinishLaunching and then decide what to do based on what you see.

The issue you're having could be caused by how you are setting the view that is first shown in your app. You might be setting the ViewController to the one that is nested into the tab view controller rather than setting it as the tab controller and then telling the tab controller which of its views to show.

That's just speculation, you'll need to share more code to get a more specific answer.

Update: Now that you have solved the problem of having the correct tab show, you want that view to segue another view on top of it.

The documentation for UITabBarController gives some hints for how to do this in the section called "The Views of a Tab Bar Controller". One way to do what you are trying to do would be to set up your UIViewControllers so that each tab has a navigation controller as a root. This way, you can push whichever controller you want onto the view in a way the user will recognize.


     UINavigationController - Controller 1 -> push new controller
    /
TBC - UINavigationController - Controller 2 -> push new controller
    \
     UINavigationController - Controller 3 -> push new controller

Alternatively, if you have a custom segue you want to use, you can continue with what you are doing with myTabBar?.selectedViewController.performSegue(withIdentifier:sender:)

rpecka
  • 1,168
  • 5
  • 17
  • Well I don't have any code.... everything I tried didn't work. However, I used .present to present the view controller, but then the tab bar controller at the bottom disappeared. I just need the same but with the tab bar controller visible at the bottom. – Luke B Apr 21 '19 at 00:03
  • @YachtSamba Show us, what you tried so far. And you’ll get an answer. – Mannopson Apr 21 '19 at 01:43
  • I'm confident that I could help you more if you provide the line of code that you used with `.present` that didn't work. @YachtSamba – rpecka Apr 21 '19 at 17:23
  • Ok - well now I have got it to go its different Tab Bar Icon, but I need to do the segue that is linked to that view. I'll put my code in. – Luke B Apr 21 '19 at 17:25
  • So is there still a problem? – rpecka Apr 21 '19 at 17:26
  • Thanks - but when I use `myTabBar?.selectedViewController.performSegue(withIdentifier:sender:)` it crashes with the error `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'segueOne''` – Luke B Apr 21 '19 at 18:21
  • That means that you haven't set up the segue on whichever view controller you are calling this function on. You need to have configured it in the storyboard. If you did set up the segue in the storyboard and you're not sure where to go from there, can you tell me about the hierarchy of your views? – rpecka Apr 21 '19 at 18:27
  • Well I set the identifier for the segue, and ctrl+dragged from the button to the other View Controller. – Luke B Apr 21 '19 at 19:11
  • So are you trying to segue when a button is pressed or when the quick action happens? – rpecka Apr 21 '19 at 20:19
  • Yes I want the segue to happen from the tab bar to the navigation controller when the button is pressed (that works) and when the quick action happens (which only takes me to the tab bar controller where there is the button) – Luke B Apr 21 '19 at 20:34
  • Are you talking about the button at the bottom of the screen that is inside the tab bar, or a button that you have created in your view controller? – rpecka Apr 21 '19 at 21:35
  • I've created a button for the segue to go to the second View Controller. – Luke B Apr 21 '19 at 21:47
  • What I've tried to do is `MyTabBar.selectedViewController?.performSegue(withIdentifier: "SegueOne", sender: nil)` after `MyTabBar.selectedIndex = 0` to perform the segue that is on the `selectedViewController`but then I get the error: `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'SegueOne''` – Luke B Apr 21 '19 at 21:49
  • Try `(MyTabBar.selectedViewController as? UINavigationController)?.topViewController.performSegue(withIdentifier: "SegueOne", sender: nil)`. The problem is probably that the segue is from your view controller, not the navigation controller – rpecka Apr 21 '19 at 21:51
  • Wow you are amazing :) - that works - thank you so much! – Luke B Apr 21 '19 at 21:53