24

In Swift I have a button created programmaticaly using:

var button = UIBarButtonItem(title: "Tableau", style: .Plain, target: self, action: "tabBarTableauClicked")

I want that when the user clicks the button it changes viewControllers. Here is the code for tabBarTableauClicked:

func tabBarTableauClicked(){
    performSegueWithIdentifier("tableau", sender: self)
}

But it is obviously not working because there is no segue with an identifier called "tableau".

And I can't create a segue using Ctrl + click the button and drag to the second viewController because the button is created programatically and not in the Storyboard.

How can I create a segue with an identifier programmatically in Swift?

vacawama
  • 150,663
  • 30
  • 266
  • 294
Clément Bisaillon
  • 5,037
  • 8
  • 32
  • 53
  • 1
    Segues can't be created outside of the storyboard. http://stackoverflow.com/questions/9674685/creating-a-segue-programmatically – user3746428 Oct 20 '14 at 00:37
  • Are your first and second view controllers in the storyboard? – vacawama Oct 20 '14 at 01:07
  • Yes they are but the button is not – Clément Bisaillon Oct 20 '14 at 01:08
  • As long as you have both viewcontrollers **in storyboard** then you can create segues there and fire them programmatically. You don't need a button in the storyboard to fire a segue, having the viewcontroller which that button resides in is enough! – mfaani Oct 19 '16 at 14:25

1 Answers1

91

Here is how to set up a segue so that it can be called programmatically.

  • Control drag from the ViewController icon in the first view controller to the second view controller.
  • Click on the segue arrow between the two view controllers, and in the Attributes Inspector on the right, give the segue an Identifier (tableau in your case).
  • Then you can trigger the segue with performSegueWithIdentifier in your code.

enter image description here

You can read more about setting up and using segues here.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • 1
    I tried this, but I'm still getting the same "has no segue with identifier 'someIdentifier' " error... – SomeGuy Feb 13 '16 at 15:32
  • 5
    @MarkLöwe, the OP wanted to trigger a segue from a button he created programmatically. He was mistaken in thinking that the segue couldn't be wired up because the button does not exist in the Storyboard. I showed him that the segue can be wired up as long as both VCs exist in the Storyboard, and then the programmatically created button can trigger that segue. I solved what he wanted to do, if not exactly what his question asked. – vacawama Feb 21 '16 at 22:46
  • @someguy you need to click the segue icon on the storyboard between your view controllers, and then in the menu on the right, give it an identifier – AlienWebguy Aug 10 '16 at 16:12
  • So *if* we had the button actually in the storyboard. Would there be any difference if the segue was made from the button or the viewController? I mean for both of them you would eventually have to do performSegueWithIdentifier which would suffice right? – mfaani Oct 19 '16 at 14:27
  • 2
    @Honey, if the segue is wired from a button, you don't need to call `performSegue(withIdentifier:sender:)` because iOS will trigger the segue for you. If you want to trigger a segue programmatically, you should wire it from the viewController. – vacawama Oct 19 '16 at 14:37
  • So you mean UIButton, has some form of a hidden mechanism that would just run something like `performSegueWithAllIdentifiers`? Interesting. Can you tell me where to look for more? – mfaani Oct 19 '16 at 14:39
  • 1
    @Honey, it performs the segue that is connected to the button, and the button is sent as the `sender`. Here is Apple's documentation. https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html – vacawama Oct 19 '16 at 14:48