user4025957
user4025957

Reputation:

Passing variable in swift with segue

I'm using segue to open a new window in a xcode app. The segue is correct, when I tap on the button the new window is shown. I want to pass a string variable from FirstViewController to DetailViewController but I can't set the variable in the DetailViewController. This is the code in FirstViewController:

if control == view.rightCalloutAccessoryView {

            func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                        let controller = segue.destination as! DetailViewController
                        controller.Name = "test"
            }

            performSegue(withIdentifier: "showdetail", sender: self)
           }

And this the DetailViewController code

class DetailViewController: UIViewController, WKNavigationDelegate {
    var Name: String = ""
     override func viewDidLoad() {
      super.viewDidLoad()
      print(Name)
    }
}

Where am I doing wrong?

Upvotes: 0

Views: 49

Answers (1)

vadian
vadian

Reputation: 285039

The method prepare(for must be on the top level of the class.

And please name variables according to the guidelines with starting lowercase letter

func someMethod()
{
    if control == view.rightCalloutAccessoryView {
       performSegue(withIdentifier: "showdetail", sender: self)
    }
}

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   let controller = segue.destination as! DetailViewController
       controller.name = "test"
   }
}

Upvotes: 2

Related Questions