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

时间:2015-10-18 10:23:50

标签: ios swift uitextfield swift2 uialertview

使用iOS9和Swift测试这个UIAlertViewController。到目前为止,我能够获得用户输入文本的弹出窗口("电子邮件地址")。实际上我也有用户输入的文本。但文本显示:可选(" test@test.com")。我提前道歉,但我现在是菜鸟。非常感谢您的提示。

@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)")
        }

    }))


}

1 个答案:

答案 0 :(得分:0)

通过添加!作为其可选项来更改操作中的代码:

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