2016-07-22 89 views
2

我發現無效返回警報但沒有找到字符串警報返回。 試圖太虛爲String迅速代碼返回警報類型字符串不工作

// return Void Alert 
func returnAlert(title: String!, message: String! ,success: (() -> Void)? , cancel: (() -> Void)?) { 
    dispatch_async(dispatch_get_main_queue(), { 
     let alertController = UIAlertController(title:title, 
      message: "", 
      preferredStyle: UIAlertControllerStyle.Alert) 

     self.newQtyField = UITextField() 
     self.newQtyField.keyboardType = .NumberPad 

     func addTextField(textField: UITextField!){ 
      // add the text field and make the result global 
      let row = self.array[Int(message)!] 
      textField.text = String(row.pbQty!) 
      self.newQtyField = textField 
     } 


     let cancelLocalized = NSLocalizedString("cancel", tableName: "activity", comment:"") 
     let okLocalized = NSLocalizedString("ok", tableName: "Localizable", comment:"") 

     let cancelAction: UIAlertAction = UIAlertAction(title: cancelLocalized, 
     style: .Cancel) { 
      action -> Void in cancel?() 
     } 
     let successAction: UIAlertAction = UIAlertAction(title: okLocalized, 
     style: .Default) { 
      action -> Void in success?() 
     } 
     alertController.addTextFieldWithConfigurationHandler(addTextField) 
     alertController.addAction(cancelAction) 
     alertController.addAction(successAction) 


     self.presentViewController(alertController, animated: true, completion: nil) 
    }) 
} 

//底部大呼過癮|我認爲成功:{()成功:{文本字段,但不工作怎麼......需要你的幫助

returnAlert("title", message: "msg", success: {() -> Void in 


     }) 
    {() -> Void in 
     print("user canceled") 
    } 
+0

你想返回newQtyField'時的'文本用戶觸摸「確定」? – t4nhpt

+0

是的,它是!就像你說的那樣。 –

回答

0

很難理解你在說你的問題是什麼。

您應方法定義改成這樣:

func returnAlert(title: String!, message: String! ,success: ((text:String) -> Void)? , cancel: (() -> Void)?) { 

,改變successAction返回的UITextField的值:

let successAction: UIAlertAction = UIAlertAction(title: okLocalized, 
      style: .Default) { action in 
       success?(text: self.newQtyField.text!) 
      } 
+0

謝謝!我完成代碼 –