iosswiftuilabelsegueckrecord

UI elements not appearing with variables coming from segue


Controller One which holds a CK Record, "Detail". One button in Controller One segues to another Controller. I have the following segue set up for that logic...

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

In Controller Two, I have the following receiving variables and the labels set to these new variables.

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
}

However, which is causing me problems, when I run this code, no labels (hooked up correctly) appear in Controller Two. As seen in the picture.

Missing Labels

What am I getting wrong here?


Solution

  • Write prepareForSegue as a separatefunction and call performSegue to invoke segue

    Please find the code in 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)
    }
    

    also please check whether these values are nil or not

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