2017-08-01 7 views
0

私は、解析データベースから取得される変数currentIDを含む次のビューにいくつかの変数を渡すセグを実行しようとしています。 performSegueは、currentIDがデータベースからダウンロードされた現在のIDに設定されるまで呼び出すべきではありません。しかし、コードを実行すると、currentIDは次のビューに渡されるときに空の文字列になります。ここでbeforeForSegue 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

あなたのボタンにあなたのセグを接続しましたか? – GIJOW

+0

次のビューに渡されるときに空の文字列ではないようにするにはどうすればよいですか? – cvrattos

答えて

0

別のコントローラからナビゲートするため、ビューコントローラからあなたセグエを接続する代わりに、ボタンからそれが動作します。

2

あなたのニーズを達成するためには、viewcontroller間でsegueを接続しなければならず、UIButtonからviewcontrollerに接続する必要はありません。

あなたはそれを呼び出す前に、あなたのセグエを準備する必要がありますたびに、これは手順です:

enter image description here

次に、それを名前を付け、使用するデリゲートメソッド

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

    } 
}