I have reproduced your code and found several mistakes, I have a working example below and think I have found your issue.
First viewcontroller (ViewController)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "show" {
let tovc = segue.destination as? AddViewController
tovc?.mess = "test"
}
}
Note: I have renamed the identifier to show
, rename it back to whatever it is at your code. Also I used the string "test"
as a replacement for your var1
In your first viewcontroller you will have to overwrite the prepare(for:sender:)
func with the code you provided. Note that the !
was one space away from the as
statement. I concatenated them and changed it into a conditional cast with ?
. This way it's more safe.
Second viewcontroller (AddViewController)
@IBOutlet weak var infoLabel: UILabel!
var mess: String = ""
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.infoLabel.text = mess
print(mess) //this is now printing 'test'
}
override func viewDidLoad() {
super.viewDidLoad()
self.infoLabel.text = mess //label is now being set
print(mess) //this is now printing 'test'
}
Note the extra _
you were missing in the viewWillAppear(animated:)
func signature. Not cohering to the signature makes it possible to not have it fired like you expect (because the system won't recognize the viewWillAppear
as a valid override).
Running this code in the latest Xcode (9.2) using Swift 4 I have no problems and can't reproduce your issue (if it ain't the missed signature of the viewWillAppear
). I'm guessing this issue is to locale if you still run into troubles.