2

I'm a swift newbie and I've got a simple question which I hope someone can help me figure out.

I have a multi-tab app. I created some segues from the tab view controllers on the Stopryboard. I've given the segues identifiers and I'm calling them from my Tab1ViewController code using performSegueWithIdentifier("tab1ToMyTarget", sender: sender) no problem.

However, I'd like to be able to call the segue from any of the app's tabs without creating new segues from the other tabs' view controllers (i.e. I don't want to create "tab2ToMyTarget" - I presume there's a better way!).

My question: Do I create these 'universal' segues on the tab bar view controller (e.g. "tabBarToTarget") (and if so how do I call it from one of my tab view controllers)? ...or...

Do I keep the segue from a single tab view controller (tab1ToTarget) and call that segue from a sibling tab view controller somehow?

James
  • 1,292
  • 1
  • 14
  • 29

2 Answers2

1

First, set the view controller that you want to go to's storyboard Id. Then run this:

let vc = storyboard!.instantiateViewControllerWithIdentifier("someViewController")
self.presentViewController(vc, animated: true, completion: nil)
Community
  • 1
  • 1
Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • Thank for this idea - sounds perfect and I wasn't aware of this method so thanks for taking the time to post the snippet. Will give it a try now :-) – James Jun 12 '16 at 20:09
  • That worked exactly as I wanted - the only thing I had to do was unwrap the storyboard `let vc = storyboard!.instantiateViewControllerWithIdentifier("testView")` (I still don't know when something's going to be an optional or not!...I'm such a n00b! :-) – James Jun 12 '16 at 20:14
  • Oh yeah! I forgot about that. I will edit my answer for future reference. – Pranav Wadhwa Jun 12 '16 at 20:15
1

You cannot use the same segue to open a controller from a different one. It's better to instantiate a view controller with its storyboard id and then present/show based on the flow.

Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23