ios Swift:使按钮触发切换到新场景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26089024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:52:38  来源:igfitidea点击:

Swift: Make button trigger segue to new scene

iosswiftxcode6

提问by skyguy

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

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

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

我知道如何创建IBAction指向按钮的链接,但是我会在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. enter image description here

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

回答by AlBlue

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 转换,最简单的方法是从按钮到视图控制器的 Control+Click 并选择一个 Segue 选项(如推)。这将为您在 IB 中连接它。

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:

如果您想自己编写代码来手动执行此操作,您可以通过命名 segue 来实现(有一个标识符选项,您可以在创建它后进行设置 - 您仍然需要在 IB 中创建 segue 才能这样做它),然后您可以使用以下代码触发它:

V2

V2

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

V3

V3

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

回答by Antonio

You can use the delegation pattern. Presuming that you have implemented a custom table cell, you can define a property in its class to hold whatever you think is helpful to identify the row - it can be its index, or (my preferred way) an instance of a class which represents the data displayed in the cell (I'm calling it MyCellData.

您可以使用委托模式。假设您已经实现了一个自定义表格单元格,您可以在它的类中定义一个属性来保存您认为有助于识别行的任何内容 - 它可以是它的索引,或者(我的首选方式)一个类的实例,它表示单元格中显示的数据(我称之为MyCellData.

The idea is to let the cell notify the table view controller about a tap on that button, passing relevant data about (the data displayed in) the row. The table view controller then launches a segue, and in the overridden prepareForSeguemethod it stores the data passed by the cell to the destination controller. This way if you have to display details data about the row, you have all the relevant info, such as the details data itself, or an identifier the destination view controller can use to retrieve the data for example from a local database or a remote service.

这个想法是让单元格通知表格视图控制器点击该按钮,传递有关(显示在其中的数据)行的相关数据。然后表视图控制器启动一个 segue,并在重写的prepareForSegue方法中存储单元格传递给目标控制器的数据。这样,如果您必须显示有关该行的详细信息数据,您就拥有所有相关信息,例如详细信息数据本身,或目标视图控制器可用于检索数据的标识符,例如从本地数据库或远程服务中检索数据.

Define a protocol:

定义一个协议:

protocol MyCellDelegate {
    func didTapMilk(data: MyCellData)
}

then declare a property named delegatein the cell class, and call its didTapMilkmethod from the IBAction

然后声明一个delegate在cell类中命名的属性,并didTapMilk从IBAction中调用它的方法

class MyTableCell : UITableViewCell {
    var delegate: MyCellDelegate?
    var data: MyCellData!

    @IBAction func didTapMilk() {
        if let delegate = self.delegate {
            delegate.didTapMilk(self.data)
        }
    }
}

Next, implement the protocol in your table view controller, along with an override of prepareForSegue

接下来,在您的表视图控制器中实现协议,并覆盖 prepareForSegue

extension MyTableViewController : MyCellDelegate {
    func didTapMilk(data: MyCellData) {
        performSegueWithIdentifier("mySegueId", sender: data)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "mySegueId" {
            let vc: MyDestinationViewController = segue.destinationViewController as MyDestinationViewController
            vc.data = sender as? MyCellData
        }
    }
}

Of course you need a dataproperty on your destination view controller for that to work. As mentioned above, if what it does is displaying details about the row, you can embed all required data into your MyCellDataclass - or at least what you need to retrieve the data from any source (such as a local DB, a remote service, etc.).

当然,您需要data在目标视图控制器上有一个属性才能工作。如上所述,如果它的作用是显示有关行的详细信息,您可以将所有必需的数据嵌入到您的MyCellData类中 - 或者至少您需要从任何来源(例如本地数据库、远程服务等)检索数据.)

Last, in cellForRowAtIndexPath, store the data in the cell and set its delegateproperty to self:

最后,在 cellForRowAtIndexPath 中,将数据存储在单元格中并将其delegate属性设置为self

extension MyTableViewController : UITableViewDataSource {
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let data: MyCellData = retrieveDataForCell(indexPath.row) // Retrieve the data to pass to the cell
        let cell = self.tableView.dequeueReusableCellWithIdentifier("myCellIdentifier") as MyTableCell
        cell.data = data
        cell.delegate = self

        // ... other initializations

        return cell
    }
}

回答by Aks

Use self.performSegueWithIdentifier("yourViewSegue", sender: sender)under your event for handling button's click:

self.performSegueWithIdentifier("yourViewSegue", sender: sender)在您的事件下使用 处理按钮的点击:

@IBAction func redButtonClicked(sender: AnyObject) {
    self.performSegueWithIdentifier("redView", sender: sender)
}

In the above code, redViewis the segue identifier.

在上面的代码中,redView是segue标识符。