Reputation: 345
I'm having trouble passing a string (StringVar) between view controllers using prepareforSegue. I've studied various tutorials on this subject, but I'm still unsuccessful (I'm a novice, so the problem might be something obvious). My code is structured as follows:
Originating ViewController:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Prepare For Segue:
func prepareforSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var SVC = segue.destinationViewController as! StudyViewController
SVC.StringVar = "Hello"
}
Destination ViewController:
class StudyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var StringVar = String()
//intervening code [omitted]
override func viewDidLoad() {
super.viewDidLoad()
print("StringVar = ", StringVar)
// Do any additional setup after loading the view.
}
The print statement in the viewDidLoad function works fine, but StringVar remains empty. Any ideas? Thanks.
Upvotes: 0
Views: 195
Reputation: 19954
What @michael was saying is that your prepareForSegue needs to use the override keyword like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//... code
}
Xcode should've corrected to this for you, so make sure you check your spelling. If you don't include the override
keyword then you are simply defining a new function named prepareforSegue
.
You can download a working example of prepareForSegue in action from my tutorial here:
Upvotes: 1