1

I am trying to implement 3d Touch and I want to navigate to a specific viewController in a Master - Detail app.

The viewController I am aiming to get to is the third controller in the hierarchy:

MasterViewController ----> SettingsViewController ----> ThirdViewController

I can access the ThirdViewController in normal operation through TabBarItem (connected via segue to SettingsViewController right from the IB) and then via a UIButton (connected via segue to ThirdViewController right from the IB) to the ThirdViewController.

i.e in MasterViewController ---> (tap barButtonItem) ----> SettingsViewControler ----> (tap uiButton) ---> ThirdViewController.

I have already amended the info.plist and 3D touch works fine and can call my action:

 func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

        var shortcutDictionary = shortcutItem.userInfo;
        let shortcutString1 = shortcutDictionary!["key1"] as! String

        if (shortcutString1 == "value1") {

      // how do i get from here to the settingsController??

        }

    }

EDIT:

already have tried:

let viewController = MasterViewController()    viewController.settingsButton.sendActionsForControlEvents(.TouchUpInside)

which gives me an error:fatal error trying to unwrap an optional value!

although this exists in MasterViewController

 @IBOutlet weak var settingsButton: UIButton!
George Asda
  • 2,119
  • 2
  • 28
  • 54
  • 1
    You'll have to push it in programmatically, which means each view controller would need to call performSegueWithIdentifier on viewWillAppear. You can tell it not to animate so that it seems instantaneous, however, you'd have to pass in a variable with each push telling it to push its next segue. There are probably better patterns for this, but this is what comes to mind for a quick implementation. – Matt Long Dec 03 '15 at 15:53
  • @Matt Long that's exactly what I've done. Thank you for your comment. – George Asda Dec 03 '15 at 16:34
  • Cool. Let know if you think of a better way to implement it. – Matt Long Dec 03 '15 at 17:17
  • @MattLong and thank you for your idea about "not animating" the segue. It actually makes a huge difference. It looks very cool because now it is as you pointed "instantaneous".. I actually didn't think of that in my initial implementation. So I have added two segues - duplicates - with different ID's (one when shortcut is called - no animation - and another with animation when in normal operation)... thanks again... – George Asda Dec 03 '15 at 22:56

1 Answers1

0

This is the code I use for the shortcut....

func actionFromShortcut(sender: AnyObject!) {


        let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue(), {


            self.performSegueWithIdentifier("shotcutToSettings", sender: nil)

//some other stuff here.....


        })

Also as per @MattLong's comment I have duplicated the segues to my controllers (one with and one without animation). That way the shortcut transition seems simultaneous when the action is called from the shortcut.

You will notice a delay of 0.1. It is to give the MasterViewController time to load cause whenever I tried to call the shortcut from cold (app not running) I would end up with the viewController I requested but there would be an empty MasterViewController to return to and hence crash....

George Asda
  • 2,119
  • 2
  • 28
  • 54