如何创建可以从以编程方式创建的按钮调用的 segue? [英] How do I create a segue that can be called from a button that is created programmatically?

查看:31
本文介绍了如何创建可以从以编程方式创建的按钮调用的 segue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Swift 中,我使用以下方法以编程方式创建了一个按钮:

In Swift I have a button created programmaticaly using:

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

我希望当用户单击按钮时它会更改 viewControllers.下面是 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)
}

但它显然不起作用,因为没有带有名为tableau"的标识符的 segue.

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

而且我无法使用 Ctrl + 单击按钮并拖动到第二个 viewController 来创建 segue,因为该按钮是通过编程方式创建的,而不是在 Storyboard 中.

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.

如何在 Swift 中以编程方式创建带有标识符的 segue?

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

推荐答案

这里是如何设置 segue 以便以编程方式调用它.

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

  • 控制从第一个视图控制器中的 ViewController 图标拖动到第二个视图控制器.
  • 点击两个视图控制器之间的 segue 箭头,然后在右侧的 Attributes Inspector 中,给 segue 一个标识符(tableau 在你的情况下).
  • 然后,您可以在代码中使用 performSegueWithIdentifier 触发转场.
  • 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.

您可以在此处阅读有关设置和使用 segue 的更多信息.

这篇关于如何创建可以从以编程方式创建的按钮调用的 segue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆