Instance member ” cannot be used on type ” prepareForSegue

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:

Picture

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?
}


Source: ios

Leave a Reply