Home Ask Login Register

Developers Planet

Your answer is one click away!

onemillion February 2016

3D touch shortcut to a tab view and then performing a segue

I am having an issue using the 3D touch shortcuts inside my application. My app uses tabs but I would like to redirect the user into a tab then also into another segue when they press the create wish list button.

Here is a picture of my storyboard.

The code I am using at the moment displays the home view controller but I would like it to go into the create wish list controller.

The code I have for the handle shortcut inside the app delegate is here:

   func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
            print("Handling shortcut")

            var succeeded = false

            if( shortcutItem.type == "com.example.Giftr" ) {

                print("- Handling \(shortcutItem.type)")

                if let tabVC = self.window?.rootViewController as? UITabBarController{
                tabVC.selectedIndex = 1 
                //This is where I need to swap to the "createwishlist" view controller. 


Pano Katsourakis February 2016

This should be able to change the ViewController to your desired class after you successfully switch tabs.

let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)

onemillion March 2016

To solve this I used a Global Variable to store that the shortcut had been taken inside the appDelegate like below.

          GlobalVars.shortcut = 1
            let tabVC = window?.rootViewController as! UITabBarController
            tabVC.selectedIndex = 0

Then inside the controller of the tab for the selectedIndex 0 checked if that value was 1 and if it was then segued to the view controller that I wanted to end up at. Like shown.

override func viewDidAppear(animated: Bool) {    
        if(GlobalVars.shortcut == 1)
            self.performSegueWithIdentifier("shortcut", sender: self)
            GlobalVars.shortcut = 0

Make sure that you set the Global Variable to 0 or this will be called every time the view appears.

Post Status

Asked in February 2016
Viewed 3,218 times
Voted 4
Answered 2 times


Leave an answer

Quote of the day: live life