我似乎无法使用SWIFT中的UIAlertViewController从UITextField获得输入值?

问题描述:

使用iOS9和Swift来测试这个UIAlertViewController。到目前为止,我能够获得用户输入文本的弹出框(“电子邮件地址”)。实际上,我也有用户输入的文字。但文本显示:可选(“[email protected]”)。我提前道歉,但我现在是新手。提前非常感谢您的提示。我似乎无法使用SWIFT中的UIAlertViewController从UITextField获得输入值?

@IBAction func UserForgotPasswordAction(sender: AnyObject) { 

func handleCancel(alertView: UIAlertAction!) 
    { 
     print("Cancelled !!") 
    } 

    let alert = UIAlertController(title: "Enter your Email Address", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
    UIAlertViewStyle.PlainTextInput 
    alert.addTextFieldWithConfigurationHandler({(textField: UITextField) in 
     print("generating the TextField") 
     textField.placeholder = "Email address" 
     textField.textAlignment = NSTextAlignment.Center 

     print("THE Input 01: \(textField.text)") 
    }) 
    self.presentViewController(alert, animated: true, completion: { 
     print("completion block") 
    }) 

    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:handleCancel)) 

    alert.addAction(UIAlertAction(title: "Reset", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in 

     if let textField2 = alert.textFields!.first { 
      print("THE Input: \(textField2.text)") 
      PFUser.requestPasswordResetForEmailInBackground("\(textField2.text)") 
     } 

    })) 


} 

更改内部动作的代码加入!作为其可选:

if let textField2 = alert.textFields!.first { 
     print("THE Input: \(textField2.text!)") 
     PFUser.requestPasswordResetForEmailInBackground("\(textField2.text!)") 
    } 
+0

完美!非常感谢!! – Jignesh