-3

I want to take a variable nomeLabel in the func goToSecondView and display it on a 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)")
    }
}

how can I do ?

Abizern
  • 146,289
  • 39
  • 203
  • 257
Milena
  • 29
  • 1
  • 8
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – A. R. Younce Nov 23 '14 at 18:23

1 Answers1

0

You need to implement prepareForSegue where you have access to the destination view controller. Here is an example on how to do that:

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
        }
    }
zisoft
  • 22,770
  • 10
  • 62
  • 73
  • but i want to take nomeLabel = newButton.titleLabel!.text – Milena Nov 23 '14 at 18:20
  • Create an outlet to your button and connect it in InterfaceBuilder. I have updated my answer. You should get the idea. – zisoft Nov 23 '14 at 18:24
  • i have a error on secondViewController.nomeLabel = self.newButton.titleLabel.text instead first i put let newButton = sender as UIButton nomeLabel = newButton.titleLabel!.text – Milena Nov 23 '14 at 18:55