簡體   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