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

风格不统一 提交于 2019-11-26 06:38:08

问题


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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/26456989/how-do-i-create-a-segue-that-can-be-called-from-a-button-that-is-created-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!