【问题标题】:How to check UITextField's text that created in UIAlertController?如何检查在 UIAlertController 中创建的 UITextField 的文本?
【发布时间】:2015-12-03 15:18:02
【问题描述】:

我创建了一个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。 有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: ios swift uitextfield uialertcontroller


    【解决方案1】:

    嗯,为了解决你的问题,只需使用我的小代码片段。在确定按钮上,您可以简单地检查字段的值:

            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)
    

    【讨论】:

    • 谢谢,您的代码成功了!我认为在代码上方定义UITextFields和单独创建UIAlertActions是这里的重点。
    • 我还有一个问题。我怎样才能像这样创建UIAlertControllertheappguruz.com/app/uploads/2015/06/screen21.png
    • @Faruk UIActionSheet ui 元素
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多