2015-10-09 41 views
0

好吧,我如何才能在添加它的塊的末尾訪問「textField」?我試圖創建一個名爲「alertTextField」的全局屬性,然後將其設置爲塊中的textField,以便我可以在其他操作中訪問它,但似乎沒有工作。有什麼建議麼?用快速解析來重置密碼

謝謝!

@IBAction func resendPassword(sender: AnyObject) { 

    let alertController : UIAlertController = UIAlertController(title: "Forgot Password?", message: "Enter your email", preferredStyle: UIAlertControllerStyle.Alert) 

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in 

    } 

    alertController.addAction(cancelAction) 

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in 

     textField == self.alertTextField 

     textField.delegate = self 
     textField.placeholder = "Email" 
    } 

    let sendAction: UIAlertAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.Default) { action -> Void in 

     if (self.alertTextField?.text != nil) { 

     PFUser.requestPasswordResetForEmailInBackground(self.alertTextField!.text) 

     } 

    } 

    alertController.addAction(sendAction) 

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

} 

回答

0

退房示例代碼查看詳情:http://nshipster.com/uialertcontroller/

@IBAction func resendPassword(sender: AnyObject) { 

    let alertController : UIAlertController = UIAlertController(title: "Forgot Password?", message: "Enter your email", preferredStyle: UIAlertControllerStyle.Alert) 

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in 
     textField.placeholder = "Email" 
    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 

    alertController.addAction(cancelAction) 

    let sendAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.Default) { _ in 
     // below is how you access the textField 
     let emailTextField = alertController.textFields![0] as UITextField 
     if let email = emailTextField.text { 
      PFUser.requestPasswordResetForEmailInBackground(email) 
     } 
    } 

    alertController.addAction(sendAction) 

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

} 
+0

完美!謝謝 – Echizzle

+0

@Echizzle如何接受答案http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –