Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

swift - How do I create a segue that can be called from a button that is created programmatically?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...