如何检查在UIAlertController中创建的UITextField文本?

时间:2015-12-03 15:18:02

标签: ios swift uitextfield uialertcontroller

我在该控制器中创建了UIAlertController而不是创建UITextFields。当我尝试将输入的文本验证到文本字段时,它们返回nil。这是代码:

 let loginWithMailAlert = UIAlertController(title: "Log In with Your Mail Address", message: "Please Enter Your E-Mail Address and Your Password", preferredStyle: UIAlertControllerStyle.Alert)
        loginWithMailAlert.addTextFieldWithConfigurationHandler({[weak self](usernameField: UITextField!) in
            usernameField.delegate = self
            usernameField.placeholder = "E-Mail Address"
        })
        loginWithMailAlert.addTextFieldWithConfigurationHandler({[weak self](passwordField: UITextField!) in
            passwordField.delegate = self
            passwordField.placeholder = "Password"
            passwordField.secureTextEntry = true
        })
        loginWithMailAlert.addAction(UIAlertAction(title: "Log In", style: UIAlertActionStyle.Default, handler:{
            (loginWithMailAlert: UIAlertAction!) in
            print("Pressed 'Log In'")
            if self.usernameField.text == "faruk" && self.passwordField.text == "123" {
                self.performSegueWithIdentifier("loginToMain", sender: nil)
            }else{
                print("Error")
                print("Username::::\(self.usernameField.text as String!)")
            }
        }))
        loginWithMailAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:{
            (loginWithMailAlert: UIAlertAction!) in
            print("Pressed 'Cancel'")

        }))

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

这是输出:

Pressed 'Log In'
Error
Username::::

我在课堂上添加了UITextFieldDelegate。 有人可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:1)

嗯,为了解决你问题,请使用我的小代码snipet。在ok按钮上,您只需检查字段的值:

        var loginTextField: UITextField?
        var passwordTextField: UITextField?

        let alertController = UIAlertController(title: "UIAlertController", message: "UIAlertController With TextField", preferredStyle: .Alert)
        let ok = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
            //Check string hear !!
            println(loginTextField?.text)
        })
        let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in
            println("Cancel Button Pressed")
        }
        alertController.addAction(ok)
        alertController.addAction(cancel)
        alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
            // Enter the textfiled customization code here.
            loginTextField = textField
            loginTextField?.placeholder = "User ID"
        }
        alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
            // Enter the textfiled customization code here.
            passwordTextField = textField
            passwordTextField?.placeholder = "Password"
            passwordTextField?.secureTextEntry = true
        }

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