iOS swift prepareForSegue无法在两个viewcontroller之间传递数据

时间:2016-11-03 03:16:53

标签: ios swift

这是我的两个viewcontroller的代码,需要从class bookTwo到classThree传递“numberToDisplay”,但它不起作用,总是在bookThree中显示0。 我在ios 10,xocod 8上使用swift3。 bookTwo:

import UIKit
import Foundation
class bookTwo: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

 func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ms1"{
        let destinationVC = segue.destination as? bookThree;
        destinationVC?.numberToDisplay = 7

    }
  }
}

bookThree

import UIKit

class bookThree: UIViewController {
var numberToDisplay = 0

    @IBOutlet weak var showType: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
  showType.text = "Tapped \(numberToDisplay) times."
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

1 个答案:

答案 0 :(得分:1)

在Swift 3中,方法的签名" prepareForSegue"已经改变。所以替换" prepareForSegue"的代码有了这个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ms1"{
            let destinationVC = segue.destination as? bookThree
            destinationVC?.numberToDisplay = 7

        }
    }