3

I am having some problems with enabling a button when you write something in a TextField. I got it to disable if a there's nothing in the TextField but I can't make it to enable again when you write something in it.

I have something like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSUInteger textLength = [_Name.text length];
    [_doneButton setEnabled:(textLength > 0)];

}
SnareChops
  • 13,175
  • 9
  • 69
  • 91

7 Answers7

9

Set delegate to the UITextField and create a method for text change in ViewDidLoad as follows.

[self.textFieldTemp addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

And add the method as follows.

- (void)textFieldDidChange:(UITextField *)textField {
    if(textField == self.textFieldTemp) 
        self.buttonTemp.enabled = ([self.textFieldTemp.text length] > 0) ? YES : NO; }
Paramasivan Samuttiram
  • 3,728
  • 1
  • 24
  • 28
  • 1
    This was the best solutions. Other answer don't work in some cases like when 1 character is entered. But this ones nails it. – Abdullah Umer Jul 17 '14 at 22:41
  • This is the best solution in my opinion. Especially helpful if you have a number of UITextFields whose state may determine whether the button should be enabled i.e. username & password pair for example – wuf810 May 21 '15 at 18:06
2

Try this

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]>0){
    [doneButton setEnabled:YES];        

}
else{
 [doneButton setEnabled:NO ;
}

Hope this May be help you..

Ben10
  • 3,221
  • 2
  • 34
  • 61
  • This needs to be placed in the `shouldChangeCaractersInRange:(NSRange)` Placing in `viewDidLoad` will only get called once when the page loads and then never again. – SnareChops Dec 20 '12 at 05:54
  • @SKM, I tried your code and it did not work. Should i add some more adjustments for this to work? – Paramasivan Samuttiram Dec 20 '12 at 06:09
  • @Santiago, how did you make it work? Please let me know, Thank you. – Paramasivan Samuttiram Dec 20 '12 at 06:10
  • 2
    This isn't the solution. When `textField:shouldChangeCharactersInRange:replacementString:` gets called, the text isn't changed yet, hence the "should" part of the name. If you check the length of `[textField.text stringByReplacingCharactersInRange:range withString:string]` it will work. But you should only do that check if you will return `YES`. – Guido Hendriks Jun 11 '13 at 15:02
1

Use UITextField delegate method to enable your button again. When your textField characters are changed, delegate method will be called, enable your button here.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     _doneButton.enabled = YES;
}
arthankamal
  • 6,341
  • 4
  • 36
  • 51
1

Put the lines in textfield delegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

It'll work then. This method is called every time you input a character in textfield. But ViewDidLoad is called only when the viewcontroller is loaded.

Hope it will work for you.

iCreative
  • 1,499
  • 1
  • 9
  • 22
0

These two functions will enable and disable your button according the text in your textfield

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     _doneButton.enabled = YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if([textField.text length]>0){
   [doneButton setEnabled:YES];        
}
else{
 [doneButton setEnabled:NO ;
}
Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
0

You can use delegates of UITextField to enable and disable the button like this.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
{
if (textField.text.length!=0)
{
yourButton.enabled = YES;
}
else
{
yourButton.enabled = NO;
}
}
ask4asif
  • 676
  • 4
  • 10
0

The method I used didn't use the delegates at all. I created this method in the controller class:

- (IBAction)textFieldChanged:(id)sender
{
    self.doneButton.enabled = ([self.Name.text length] > 0) ? YES : NO;
}

Then, in the Interface Builder, I set the text field "Editing Changed" action to target the new method. Then, similar to @Paramasivan Samuttiram, I added an extra call to it in the viewDidLoad:.

[self textFieldChanged:self.Name];

That part allows the button to be in the correct state initially.

Erik Allen
  • 1,841
  • 2
  • 20
  • 36