繁体   English   中英

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

[英]iOS swift prepareForSegue cannot pass data between two viewcontroller

这是我的两个viewcontroller的代码,需要从classbookTwo到classbookThree传递“ 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

    }
  }
}

书三

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

在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

        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM