开发者

disable a UIButton until there is text in the UITextField

How do i disable a UI开发者_如何学GoButton until there is text in the UITextField. The code i have doesn't seem to work.

    if ([nameTextField.text isEqualToString: @""]) {
            [myButton setEnabled:NO];
}


Here's the code from kingthong's blog post, which worked for me:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.saveButton setEnabled:NO];

    [[self myTextField] becomeFirstResponder];
    [myTextField addTarget:self 
                    action:@selector(textFieldDidChange)
          forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldDidChange
{
    if ([self.myTextField.text isEqualToString:@""]) {
        [self.saveButton setEnabled:NO];
    }
    else {
        [self.saveButton setEnabled:YES];
    }
}


Keep the button disabled. Implement the UITextFieldDelegate and implement the method:

- (void)textFieldDidBeginEditing:(UITextField *)textField;       

Check the length of the text in the textField and if it is greater than 0, enable the button.


First disable the button then use this:

if (![nameTextField.text isEqualToString: @""]) {
            [myButton setEnabled:YES];
}


I would double check to make sure that the if statement is being entered - sometimes things can be a little tricky with outlets.

Another way you could achieve similar results is to use the following methods.

– addTarget:action:forControlEvents:
– removeTarget:action:forControlEvents:


This should do it. Try this,

if ([nameTextField.text isEqualToString: @""] == TRUE) {
    [myButton setEnabled:NO];
}


First check that your UITextField and UIButton are connected with interface builders UITextField and UIButton or not ?

And also write your code in below method :

- (void)textFieldDidBeginEditing:(UITextField *)textField;


set it disabled at allocation time and then use textField delegate method with condition .

-(void)textFieldDidBeginEditing:(UITextField *)textfield {
 if(yourButton isEqual:@"") {
    yourButton.enabled = NO;
 }else {
    your.enabled = YES;
 }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜