2017-02-01 65 views
-1

控制器一個持有CK記錄,「細節」。控制器1中的一個按鈕會跳到另一個控制器。我有以下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

什麼我收到錯在這裏?

+0

增加@ Nirav-D答案。這是正確的。第一 - 你應該**從不**結束函數中的函數(就像你有)。其次(不太重要)現在,Swift使用Dictionary,除非有特定的NSDictionary原因。 – dfd

回答

0

寫prepareForSegue作爲separatefunction並呼籲performSegue調用賽格瑞

請查閱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 
+0

這兩個值都是非零。 – Jon

+0

價值保留怎麼樣,在控制器中採取FoodDetail01 –

+0

字符串 - 「是」或「否」是預期的響應。這些值已填寫在CloudKit儀表板中。 – Jon

1

您在booton動作中添加了prepareForSegue,但它應該與您的其他課程級別方法一起使用。

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

注:在斯威夫特是連擊,如果你使用本地Dictionarysubscript而不是NSDictionary

0

請更新您的代碼,以便在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! 
    }... 
相關問題