使用prepareForSegue传递数据 [英] Pass data with prepareForSegue

查看:96
本文介绍了使用prepareForSegue传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从viewController 1传递到viewController2,我有2个按钮和1个segue(因此有一个segue标识符),这2个按钮应被按下,每个按钮应显示:1个标签以显示标题和1个textView为了显示一个定义,我很难显示每个单词自己的数据;我知道它必须是performSegueWithIdentifier中引用SENDER的一些代码,但我不知道该怎么做.

Im trying to pass data from viewController 1 to viewController2, I have 2 buttons and 1 segue(therefore there is one segue identifier) for those 2 buttons, each button when pressed should show: 1 label to show the title and 1 textView to show a definition, I am having troubles to show its own data of each word; I know it has to be the some code referencing the SENDER in the performSegueWithIdentifier, but I don't know how to do it.

感谢您的帮助!!!谢谢.

I appreciate your help !!! thanks.

这是我的代码

class ViewController: UIViewController {

    @IBAction func AbstractionBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
    }

    @IBAction func binarySystemBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Binary System")

    }

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ShowDefinition") {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController {
                destinationViewController.titleMsg = "Abstraction"
                destinationViewController.definitionMsg = "Abstraction Definition"
            }
    } else if(segue.identifier == "ShowDefinition"){if let destinationViewController = segue.destinationViewController as? EnglishViewController {
            destinationViewController.titleMsg = "Binary System"
            destinationViewController.definitionMsg = "Binary System Definition"
            }
        }    
}

推荐答案

您已正确地将定义作为字符串传递给了performSegueWithIdentifier的sender参数.您只需要在prepareForSegue中使用它的值,但首先必须从AnyObject强制转换它?回到字符串.

You have correctly passed the definition as a String in the sender parameter in performSegueWithIdentifier. You just need to use its value in prepareForSegue, but first you must cast it from AnyObject? back to a String.

您的代码可能类似于:

class ViewController: UIViewController {
    @IBAction func AbstractionBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
    }

    @IBAction func binarySystemBtn(sender: AnyObject) {
        performSegueWithIdentifier("ShowDefinition", sender: "Binary System")
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "ShowDefinition") {
            if let destinationViewController = segue.destinationViewController as? EnglishViewController {
                if let definition = sender as? String {
                    if definition == "Abstraction" {
                        destinationViewController.titleMsg = "Abstraction"
                        destinationViewController.definitionMsg = "Abstraction Definition"
                    } else if definition == "Binary System" {
                        destinationViewController.titleMsg = "Binary System"
                        destinationViewController.definitionMsg = "Binary System Definition"
                    }
                }
            }
        }
    }
}

这篇关于使用prepareForSegue传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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