Reputation: 491
I've tried to understand this error before. I hope something is not initialized yet but I can't to figure out how to fix the problem.
This is wrong in prepareForSegue
:
Instance member 'choice' cannot be used on type lastVC
I have the following app:
The idea is to store the option chosen in each view and send information to the lastVC
throughout prepareForSegue
. I have this code but it's not working just it like should.
class VC1:
UIViewController {
var choice : MyChoices?
@IBOutlet weak var nextOutlet: UIButton!
@IBOutlet weak var colourLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
nextOutlet.hidden = true
}
@IBAction func redButton(sender: AnyObject) {
nextOutlet.hidden = false
colourLabel.text = "Red colour selected"
choice.color = "Red"
}
@IBAction func blueButton(sender: AnyObject) {
nextOutlet.hidden = false
colourLabel.text = "Blue colour selected"
choice.color = "Blue"
}
@IBAction func greenButton(sender: AnyObject) {
nextOutlet.hidden = false
colourLabel.text = "Green colour selected"
choice.color = "Green"
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let lastVC = segue.destinationViewController as! lastVC
lastVC.choice = self.choice
}
}
VC2 and VC3 are the same as VC1 (same outlets and buttons)
lastVC {
var choice : MyChoices?
@IBOutlet weak var colourLabel: UILabel!
@IBOutlet weak var styleLabel: UILabel!
@IBOutlet weak var sizeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
And the last class to store the strings
import UIKit
class MyChoices {
var colour : String?
var style : String?
var size : String?
}
Upvotes: 0
Views: 1117
Reputation: 11
try to init a new instance of myChoices and try to pass this to lastVC.choice
And: you should add a convenient init to your myChoices class or make a struct instead
Upvotes: 0
Reputation: 1567
You should check your segue identifier in prepareForSegue
method before cast the lastVC. I think your prepareForSegue
method tries to cast your all VCs to lastVC so it crashes every time. Try to check your segue identifier.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegueIdentifier" {
let lastVC = segue.destinationViewController as! lastVC
lastVC.choice = self.choice
}
}
Upvotes: 1