Swift 1.2 - 针对View Controller嵌入式导航控制器的prepareForSegue

时间:2015-09-07 11:10:35

标签: ios swift

this问题中,我需要将一个值从ViewController传递到另一个,第二个VC嵌入在导航控制器中。我尝试了这个问题的答案,但在控制台中打印的值总是为。无法弄清楚该怎么做。

在First VC中我有:

var dataToSend : String!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

        if segue.identifier == "toSecondVCSegue" {

            println("prepareForSegue occurred test value is: *\(dataToSend)*")

            let destinationNavigationController = segue.destinationViewController as! UINavigationController
            let targetController = destinationNavigationController.topViewController as! SecondViewController

            targetController.receivedTest = dataToSend

        }
    }




    @IBAction func toSecondVCButtonTapped(sender: UIButton) {

        performSegueWithIdentifier("toSecondVCSegue", sender: nil)

        dataToSend = "passed"
    }

在第二个我有:

var receivedTest : String!

    override func viewDidLoad() {
        super.viewDidLoad()

        println("in SecondViewController in viewDidLoad receivedTest is: *\(receivedTest)*")

    }

    override func viewWillAppear(animated: Bool) {

        println("in SecondViewController in viewWillAppear receivedTest is: *\(receivedTest)*")

    }


    override func viewDidAppear(animated: Bool) {

        println("in SecondViewController in viewDidAppear receivedTest is: *\(receivedTest)*")

    }

1 个答案:

答案 0 :(得分:1)

我认为,原因是您在调用dataToSend之后将值设置为performSegueWithIdentifier变量,因此它始终为零

尝试将代码更改为:

@IBAction func toSecondVCButtonTapped(sender: UIButton) {
    dataToSend = "passed"
    performSegueWithIdentifier("toSecondVCSegue", sender: nil)
}

这可能有所帮助!