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

时间:2015-02-27 13:52:54

标签: ios swift segue

Array类型的值应该通过forceView2中的stiffView2传递到prepareForSegueViewController1。但是,当我在forceView2中打印时,stiffView2ViewController2都是空的。

任何人都可以解释为什么它在required 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
 }

1 个答案:

答案 0 :(得分:2)

在实例化viewController时,init在此行完成:var svcViewController2 = segue.destinationViewController as springViewController2。此时您的值尚未通过。

例如,您应该尝试println() viewDidLoad方法中的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 } } 数据。

修改:

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

{{1}}

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