简体   繁体   English

检查UITextField是否为空或是否在viewDidLoad上启用按钮

[英]Check if UITextField is empty or not on viewDidLoad and enable button

I have mutiple UItextFields in a ViewController. 我在ViewController中有多个UItextFields。 Also I have outlet functions from storyboard to check if the textfields are empty or not and I am enabling a button only when the textfields are non-empty. 另外,我还有情节提要中的插座功能,用于检查文本字段是否为空,并且仅当文本字段为非空时才启用按钮。

For a few of the textfields I am setting the data before viewDidLoad so the user does not have to manually enter it. 对于某些文本字段,我在viewDidLoad之前设置了数据,因此用户不必手动输入数据。 Even though these textfields have data, they have to be clicked once so that button gets enabled. 即使这些文本字段具有数据,也必须单击一次以启用按钮。

var isFirstNameValid = false
@IBAction func validateFirstName(sender: AnyObject) {
        guard (!firstNameText.text!.isEmpty) else{
            isFirstNameValid = false
            return
        }
        isFirstNameValid = true
        enableRegisterButton()
    }


func enableRegisterButton()
    {
        guard (isFirstNameValid) else{
            registerButton.enabled = false
            return
        }
        registerButton.enabled = true
    }

I want to enable the button when the textfield has data entered by the user or programatically set before viewDidLoad method. 当文本字段具有用户输入的数据或在viewDidLoad方法之前以编程方式设置的数据时,我想启用按钮。

Your code seems be good, but i think you need to try to replace this: 您的代码似乎不错,但我认为您需要尝试替换此代码:

@IBAction func validateFirstName(sender: AnyObject) {
    guard (!firstNameText.text!.isEmpty) else{
        isFirstNameValid = false
        return
    }
    isFirstNameValid = true
    enableRegisterButton()
}

by 通过

@IBAction func validateFirstName(sender: AnyObject) {
    isFirstNameValid = !firstNameText.text!.isEmpty
    enableRegisterButton()
}

OR: 要么:

You can check in your connections inspector for a Value Changed event and add the method. 您可以在连接检查器中签入“ Value Changed事件并添加方法。

Even if it's not working, you can check for your data value in the viewWillAppear() method: 即使它不起作用,也可以在viewWillAppear()方法中检查数据值:

override func viewWillAppear(animated: Bool) {
    registerButton.enabled = !firstNameText.text!.isEmpty
}

Try this. 尝试这个。 But as you say "when the textfield has data entered by the user" so you can't do it before viewDidLoad.I think you should explain more. 但是正如您所说的“当文本字段中有用户输入的数据时”,因此您不能在viewDidLoad之前进行操作。我认为您应该解释更多。

textField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)

and

func textFieldDidChange(textField: UITextField) {
    //your code.enable your button here
}

Validate all the the textfields once in viewDidLoad() and try to enable register button. 在viewDidLoad()中一次验证所有文本字段,然后尝试启用注册按钮。 This way register button will be enabled even if all of the textfields are pre-filled without requiring you to click any of them Like this: 即使所有文本字段都已预先填写,而无需您单击其中的任何一个,都将启用此注册按钮,如下所示:

isFirstNameValid = !firstNameText.text!.isEmpty
isLastNameValid = !lastNameText.text!.isEmpty
// rest of the fileds...
enableRegisterButton()

Your validation code needs to run without user interaction and the best place to do this is in viewDidLoad . 您的验证代码需要在没有用户交互的情况下运行,而执行此操作的最佳位置是viewDidLoad Create a checker function which takes in a UITextField as its argument and does the actual checking. 创建一个检查器函数,该函数将UITextField作为其参数并进行实际检查。 This way, you can call that checker function in viewDidLoad as well as later on when you validate actual user input in your IBAction or wherever. 这样,您可以在viewDidLoad中以及以后在验证IBAction或任何位置中的实际用户输入时调用该checker函数。

override func viewDidLoad() {
    super.viewDidLoad()

    // This is where your code to set firstNameText goes

    let isFirstNameValid = checker(firstNameText)

    myButton.enabled = false
    if isFirstNameValid { registerButton.enabled = true }
}

func checker(textField: UITextField) -> Bool {
    guard (!textField.text!.isEmpty) else {
        return false
    }
    return true
}

Side Note 边注

The enableRegisterButton() function is extraneous. enableRegisterButton()函数是多余的。 You've already done the checking which is what called the function to begin with, so checking if the first name is valid inside the enableRegisterButton is redundant. 您已经完成了以函数开头的检查,因此检查名字是否在enableRegisterButton内部有效是多余的。 The only useful line in that function is registerButton.enabled = true , which we can write without using the function at all. 该函数中唯一有用的行是registerButton.enabled = true ,我们可以编写它而根本不使用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM