UI 个元素没有出现在来自 segue 的变量中

UI elements not appearing with variables coming from segue

持有 CK 记录的控制器一,"Detail"。 Controller One 中的一个按钮连接到另一个 Controller。我为该逻辑设置了以下 segue...

@IBAction func booton(_ sender: Any) {
    func prepareForSegue(segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "lastsegue"
        {
          if let destination = segue.destination as? FoodDetail01
          {
            let lastreservation = detail.value(forKey: "Reservation") as? String
            let lasttake = detail.value(forKey: "Take") as? String

            destination.reservation = lastreservation
            destination.take = lasttake
        }...

在控制器二中,我有以下接收变量和为这些新变量设置的标签。

import UIKit

class FoodDetail01: UIViewController {

     var reservation: String!
     var take: String!

@IBOutlet weak var reservationlabel: UILabel!
@IBOutlet weak var takelabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    self.reservationlabel.text = reservation
    self.takelabel.text = take
}

然而,这给我带来了问题,当我 运行 这段代码时,没有标签(正确连接)出现在控制器二中。如图所示。

Missing Labels

我哪里错了?

您已在 booton 操作中添加了 prepareForSegue,但它应该与您的其他 class 级别方法一起使用。

@IBAction func booton(_ sender: Any) {
     //Button action code
}

//prepareForSegue is outside from that method like other method of class
override func prepareForSegue(segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "lastsegue"
    {
        if let destination = segue.destination as? FoodDetail01 {
            let lastreservation = detail.value(forKey: "Reservation") as? String
            let lasttake = detail.value(forKey: "Take") as? String

            destination.reservation = lastreservation
            destination.take = lasttake
        }
        ...
    }
}

注意: 在 Swift 中,如果您使用原生 Dictionarysubscript 而不是 NSDictionary 则更好。

请更新您的代码以在 segue 中传递字符串值。 添加 !在最后一次预订和最后一次拍摄之后。如果你不添加那么它传递 nil 值。你可以通过添加断点来检查。

@IBAction func booton(_ sender: Any) {
func prepareForSegue(segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "lastsegue"
    {
      if let destination = segue.destination as? FoodDetail01
      {
        let lastreservation = detail.value(forKey: "Reservation") as? String
        let lasttake = detail.value(forKey: "Take") as? String

        destination.reservation = lastreservation!
        destination.take = lasttake!
    }...

将 prepareForSegue 编写为一个单独的函数并调用 performSegue 来调用 segue

请在SWIFT中查找代码3

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "lastsegue"
    {
      if let destination = segue.destination as? FoodDetail01
      {
        let lastreservation = detail.value(forKey: "Reservation") as? String
        let lasttake = detail.value(forKey: "Take") as? String

        destination.reservation = lastreservation
        destination.take = lasttake
      }
    }
}

@IBAction func booton(_ sender: Any) {

    self.performSegue(withIdentifier: "lastsegue", sender: self)
}

另外请检查这些值是否为零

let lastreservation = detail.value(forKey: "Reservation") as? String
let lasttake = detail.value(forKey: "Take") as? String