使用prepareForSegue传递数据

时间:2016-10-10 09:35:54

标签: ios swift swift2 sender

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

感谢您的帮助!!!感谢。

这是我的代码

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"
            }
        }    
}

3 个答案:

答案 0 :(得分:2)

您已在performSegueWithIdentifier中的sender参数中正确地将定义作为String传递。你只需要在prepareForSegue中使用它的值,但首先你必须从AnyObject中抛出它吗?回到一个字符串。

您的代码可能类似于:

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"
                    }
                }
            }
        }
    }
}

答案 1 :(得分:1)

试试这个

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

            }

        }
    }
}

并按此设置按钮标记

AbstractionBtn.tag = 1
binarySystemBtn.tag = 2

现在就像这样呼叫segue

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

答案 2 :(得分:0)

创建名为titleMsgdefinitionMsg的var。 在每个@IBAction方法中,将相应的标题消息设置为self.titleMsg,同样设置为self.definitionMsg

在那次电话会议之后,

performSegueWithIdentifier("ShowDefinition", sender: self)

然后覆盖,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ShowDefinition" {
        let destinationVC = segue.destinationViewController as? EnglishViewController
        destinationVC.titleMsg = self.titleMsg
        destinationVC.definitionMsg = self.definitionMsg
    }
}

希望有所帮助!