To enable button if all the textfields are filled in ios

此生再无相见时 提交于 2019-12-23 01:52:36

问题


I have designed a signup form in ios with four UITextFields and a UIButton. I have set the buttons' enabled property to NO by default. Now I want to enable the button only when all the four textfields are filled. Can you please help me out with this as I a new to ios, and stuck with this issue.


回答1:


A better way would be to use the didChange method like the UITextViewDelegate method, but as we know the UITextFieldDelegate does not have a didChange method. You can manually add behaviour. You can use the shouldChangeCharactersInRange: method, but personally I would advise not to override methods unless you absolutely have to.

You can add behaviour using:

[myTextField1 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];//And so on for all your text fields

And in the target method:

- (void)textFieldDidChange:(UITextField*)textField{

    if (myTextField1.text.length > 0 && myTextField2.text.length > 0 && myTextField3.text.length > 0 && myTextField4.text.length > 0){

        myButton.enabled = YES;

    } else {

        myButton.enabled = NO;
    }
}

EDIT: Further, if you want to make sure they are enabled only if the user has entered valid text, and not empty spaces, you may use the following to get the trimmed text, check if this trimmed text has length > 0:

NSUInteger textLength = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



回答2:


in your .h file

@interface YourViewController : UIViewController <UITextFieldDelegate>

in your .m file

Write this in viewDidLoad

self.btnSignUp.enable=NO;  //button is disable by default
self.textField1.delegate=self;  // set delegate of text field
self.textField2.delegate=self;
self.textField3.delegate=self;
self.textField4.delegate=self;

Write this text field's delegate method

-(void)textFieldDidEndEditing:(UITextField *)textField   
{

     if ([textField1.text length]>0 && [textField2.text length]>0 && [textField3.text length]>0 && [textField4.text length]>0)   // check if all the textfields are filled
    {
        self.btnSignUp.enabled:YES;   // enable button here
    }

}



回答3:


I know this is relatively old, but I wanna pitch in another idea. I've just implemented this using the UITextFieldTextDidChangeNotification.

Register for the notification:

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFieldDidChange:"), name: "UITextFieldTextDidChangeNotification", object: nil)

Add the method for handling the notification. Set the enabled property of the button to true only if it's true that all of your text fields are not empty. This wouldn't be a great solution with 20 text fields, but for 2 - 5 it's fine.

 func textFieldDidChange(notification: NSNotification) {
    self.button.enabled = self.firstField.text != "" && self.secondField.text != ""
}

That method gets called every time a textfield's content changes, so the button responds in real time.




回答4:


I would approach this in a slightly different way.
Instead of going through the muck of enabling/disabling the UIButton, I would let the UIButton be enabled all along and let it's targeted action method decide what to do.

Example:

//Sign Up button method
-(IBAction)btnSignUp:(UIButton *)sender
{
    //create a local array of the monitored textFields that should NOT be empty
    NSArray *arrTextFields = @[txtF1,txtF2,txtF3,txtF4];

    //helps quicken the process by using fast-enumeration as so:
    for (UITextField *txtFCurrent in arrTextFields) {
        if ([[txtFCurrent.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]) {
            NSLog(@"%@ found to be empty",txtFCurrent);

            //help direct the user to fill it
            //maybe after showing an alert but anyways...
            [txtFCurrent becomeFirstResponder];

            //don't proceed with the main button logic
            //since a required textField is empty
            return;
        }
    }

    //else...
    NSLog(@"All textfields are go... Proceed with the sign up request");
    //...
}


来源:https://stackoverflow.com/questions/25215742/to-enable-button-if-all-the-textfields-are-filled-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!