实例成员''不能用于类型''prepareForSegue

时间:2015-10-05 08:30:42

标签: ios iphone swift

我之前尝试过了解这个错误。我希望有些东西尚未初始化,但我无法弄清楚如何解决问题。

这在prepareForSegue

中是错误的
  

实例成员'选择'不能用于类型lastVC

我有以下应用:

Picture

我们的想法是存储在每个视图中选择的选项,并将信息发送到整个lastVC中的prepareForSegue。我有这个代码,但它不能正常工作。

类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和VC3与VC1(相同的插座和按钮)相同

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

最后一个存储字符串的类

import UIKit

class MyChoices {
    var colour : String?
    var style : String?
    var size : String?
}

2 个答案:

答案 0 :(得分:1)

在投射lastVC之前,您应该在prepareForSegue方法中检查您的segue标识符。我认为你的prepareForSegue方法会尝试将所有VC转换为lastVC,因此每次都会崩溃。尝试检查您的segue标识符。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "YourSegueIdentifier" {
        let lastVC = segue.destinationViewController as! lastVC
        lastVC.choice = self.choice
    }
}

答案 1 :(得分:0)

尝试初始化myChoices的新实例并尝试将其传递给lastVC.choice

并且:你应该为myChoices类添加一个方便的init或者改为构造一个struct