2017-08-01 53 views
0

我想執行一個segue,它將一些變量傳遞給下一個視圖,包括從parse數據庫中檢索的一個變量currentID。在將currentID設置爲從數據庫下載的當前ID之後,不應調用performSegue。但是,當我運行代碼時,當它傳遞給下一個視圖時,currentID最終會變成一個空字符串。prepareForSegue before performSegue

這裏被稱爲我的代碼通過按鈕:

@IBAction func submitButtonPressed(_ sender: Any) { 

    let point = PFGeoPoint(latitude:0.0, longitude:0.0) 
    let testObject = PFObject(className: "Person") 

    testObject["inputAmount"] = inputAmount 
    testObject["outputAmount"] = outputAmount 
    testObject["inputCurrency"] = inputCurrency 
    testObject["outputCurrency"] = outputCurrency 
    testObject["location"] = point 

    testObject.saveInBackground { (success, error) -> Void in 

     // added test for success 11th July 2016 

     if success { 
      print("Object has been saved.") 

      self.currentID = String(describing: testObject.objectId!) 
      if(self.currentID != ""){ 
       self.performSegue(withIdentifier: "mainToListSegue", sender: self) 
      } 

     } else { 
      if error != nil { 
       print (error) 
      } else { 
       print ("Error") 
      } 
     } 
    } 
} 

這裏是prepareForSegue方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let listViewController = (segue.destination as! UINavigationController).viewControllers[0] as! ListViewController 
    listViewController.inputCurrency = inputCurrency 
    listViewController.outputCurrency = outputCurrency 
    listViewController.inputAmount = inputAmount 
    listViewController.outputAmount = outputAmount 
    listViewController.currentID = currentID 
    listViewController.cellContent = cellContent 
} 
+0

問題是什麼? –

+2

你把你的segue連接到你的按鈕了嗎? – GIJOW

+0

當傳遞給下一個視圖時,我如何獲得currentID不是空字符串? – cvrattos

回答

0

對於從一個控制器到另一個導航,從視圖控制器連接您的SEGUE代替從按鈕,它將工作。

2

爲了實現您的需求,您必須在視圖控制器之間連接您的segue,而不是從UIButton連接到viewcontroller。

每次你需要調用它之前準備賽格瑞的時間,這是程序:

enter image description here

然後,將其命名和使用委託方法

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "mySegue" { 

    } 
}