Swift:prepareForSegue 有两个不同的 segue [英] Swift: prepareForSegue with two different segues

查看:26
本文介绍了Swift:prepareForSegue 有两个不同的 segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图上有 2 个 UIButtons.它们中的每一个都链接到 2 个不同的视图.他们必须根据您刚刚点击的按钮传递不同的数据.

I have 2 UIButtons on a view. Each of them are linked to 2 different views. They have to pass different data depending which button you just tapped.

我知道 segue 有不同的方法,但我想为每个版本使用 prepareForSegue 版本(对我来说使用 segue 似乎更清楚代码>绘制在故事板上和一些代码来解释发生了什么).

I know there is different way to segue but I want to use the prepareForSegue version for each (seems more clear for me to have the segue drawn on the storyboard and some code to explain what happened).

我试过了...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue != "historySegue") {
        let controller = segue.destinationViewController as! ResultViewController
        controller.match = self.match
    } else {
        let controller = segue.destinationViewController as! HistoryViewController
        controller.history = self.history
    }
}


@IBAction func showHistory(sender: UIButton) {
    performSegueWithIdentifier("historySegue", sender: self)
}

@IBAction func match(sender: UIButton) {
    performSegueWithIdentifier("matchSegue", sender: self)
}

但是当我点击按钮时出现错误

But I got an error when I click the buttons

(lldb)

推荐答案

取出您的 IBActions(确保通过 Interface Builder 中的右侧栏以及您的代码在 Connections Inspector 中删除它们).对于segue,只需使用Interface Builder,确保两个segue 的标识符都正确,然后试试这个prepareForSegue 方法:

Take out your IBActions (making sure to delete them in the Connections Inspector via the right side bar in the Interface Builder as well as in your code). For the segues, just use the Interface Builder, make sure both segue's identifiers are correct, then try this prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "matchSegue" {
        let controller = segue.destinationViewController as! ResultViewController
        controller.match = self.match
    } else if segue.identifier == "historySegue" {
        let controller = segue.destinationViewController as! HistoryViewController
        controller.history = self.history
    }
}

这篇关于Swift:prepareForSegue 有两个不同的 segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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