ios – Swift – prepareforsegue值不会传递到必需的init

Array类型的值应该通过在ViewController1中使用prepareForSegue传递给forceView2和stiffView2.但是当我在ViewController2中打印时,forceView2和stiffView2都是空的.

任何人都可以解释为什么它在所需的init内部时不起作用?

ViewController1

@IBAction func solve_Pressed(sender: AnyObject) {

        self.performSegueWithIdentifier("Solve", sender: sender)

    }

 override func prepareForSegue ( segue: UIStoryboardSegue, sender: AnyObject!) {

        if (segue.identifier == "Solve") {
            var svcViewController2 = segue.destinationViewController as springViewController2

            svcViewController2.forceView2 = self.force
            svcViewController2.stiffView2 = self.stiffness
        }

ViewController2

var forceView2 = [Float]()
var stiffView2 = [Float]()
var forceB1 : Float
var forceB2 : Float
var SpringD : [Float]

required init (coder aDecoder: NSCoder) {

    if(isCheckedGlobal) {
    (forceB1,forceB2,SpringD) = calculateBoundary(&forceView2, stiffView2, springNum) }

else {  println(forceView2)
        println(stiffView2)
        println(springNum)

        SpringD = calculate(forceView2, stiffView2, springNum)
        self.forceB1  = forceView2[0]
        self.forceB2 = 0.0
 }

最佳答案 当您在此行实例化viewController时完成init:var svcViewController2 = segue.destinationViewController as springViewController2.此时您的值尚未传递.

例如,您应该尝试在viewDidLoad方法中println()您的数据.

编辑:

我现在不能尝试,但你应该尝试这样的事情:

var forceView2 = [Float]()
var stiffView2 = [Float]()
var forceB1 : Float
var forceB2 : Float
var SpringD : [Float]

required init (coder aDecoder: NSCoder) {
    super.init(coder: aDecoder) 
}

override func viewDidLoad() {
    super.viewDidLoad()

    if(isCheckedGlobal) {
        (forceB1,forceB2,SpringD) = calculateBoundary(&forceView2, stiffView2, springNum) }

    else {
        println(forceView2)
        println(stiffView2)
        println(springNum)

        SpringD = calculate(forceView2, stiffView2, springNum)
        self.forceB1  = forceView2[0]
        self.forceB2 = 0.0
    }
}

如果需要,请不要忘记检查您的房产(可选,零,……).

点赞