Swift:使按钮触发segue到新场景 [英] Swift: Make button trigger segue to new scene

查看:80
本文介绍了Swift:使按钮触发segue到新场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我有一个 TableView 场景,我打算在每个单元格中放一个按钮,点击时我需要每个单元格 segue 到它自己的 ViewController 场景。换句话说,我需要知道如何将按钮连接到场景(我现在唯一的按钮是牛奶)

Alright, so I have a TableView scene and I'm going to put a button in each of the cells and I need each cell to, when clicked, segue to its own ViewController scene. In other words, I need to know how to connect a button to a scene (The only button I have right now is "milk")

我知道如何创建一个 IBAction 链接到一个按钮,但我会在 IBAction 中添加什么?

I know how to create an IBAction linked to a button, but what would I put in the IBAction?

我是初学者,所以我需要一步一步的解释。我已经包含了我的故事板的图片。我还没有编写任何代码。

I'm a beginner so I need a step-by-step explanation here. I've included a picture of my storyboard. I haven't written any code yet.

推荐答案

如果你想让一个按钮触发segue过渡,最简单的办法就是从按钮到视图控制器的Control + Click并选择一个Segue选项(如push)。这将在IB中为您连接。

If you want to have a button trigger the segue transition, the easiest thing to do is Control+Click from the button to the view controller and choose a Segue option (like push). This will wire it up in IB for you.

如果您想手动编写代码来执行此操作,可以通过命名segue(有一个标识符)来实现一旦你创建了它就可以设置的选项 - 你仍然需要在IB之前创建segue然后你可以用这个代码触发它:

If you want to write the code to do this yourself manually, you can do so by naming the segue (there's an identifier option which you can set once you've created it - you still need to create the segue in IB before you can do it) and then you can trigger it with this code:

V2

@IBAction func about(sender: AnyObject) {
    performSegueWithIdentifier("about", sender: sender)
}

V3

@IBAction func about(_ sender: Any) {
    performSegue(withIdentifier: "about", sender: sender)
}

这篇关于Swift:使按钮触发segue到新场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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