Swift采取变量

时间:2014-11-23 18:10:39

标签: swift

我想在func goToSecondView中使用变量nomeLabel并在SecondView上显示它

class ViewController: UIViewController {
    var nomeLabel:String!

    @IBAction func goToSecondView(sender: AnyObject) {
        self.performSegueWithIdentifier("goToSecondView", sender: self)
        let newButton = sender as UIButton
        nomeLabel = newButton.titleLabel!.text
        println("nomeLabel \(nomeLabel)")
    }
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要实现prepareForSegue,您可以访问目标视图控制器。以下是如何执行此操作的示例:

class ViewController: UIViewController {
    @IBOutlet var newButton: UIButton!

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

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "goToSecondView" {
            let secondViewController = segue.destinationViewController as SecondViewController
            secondViewController.nomeLabel = self.newButton.titleLabel.text
        }
    }