好吧,那么我如何才能访问添加它的 block 末尾的“textField”呢?我尝试创建一个名为“alertTextField”的全局属性,然后将其设置为 block 中的 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)
}
最佳答案
查看此处的示例代码以了解详细信息: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)
}
关于swift - 在 swift 中使用 parse 重置密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048438/