Question

i have a little problem. There are two textFields in my TableView which set the property of an object. In order to do so i want to force the user to write something in the textField before the string is actually been set to the object. So basically a simple ([textField.text length] > 0) thing. But i want that the user have to write strings in both the two textFields to finally enable the "Done"-Button. I solved this earlier but with only one text Field with the following UITextFieldDelegate method.

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
        self.doneBarButton.enabled = ([newText length] > 0);
        return YES;
    }

My solution for the new problem, so now with two textFields is this one:

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];

    if ([theTextField.placeholder isEqualToString:@"textField1"]) {
        if ([theTextField.text length] > 0) {
            enabledVokabel = YES;
        } else {
            enabledVokabel = NO;
        }
    }
    if ([theTextField.placeholder isEqualToString:@"textField2"]) {
        if ([theTextField.text length] > 0) {
            enabledUebersetung = YES;
        } else {
            enabledUebersetung = NO;
        }
    }

    self.doneBarButton.enabled = (enabledVokabel && enabledUebersetung);
    return YES;
}

So i want the doneBarButton been enabled when both of the textFields (textField1 and textField2) are filled with text. But i want it that way that if the user has deleted the text he/she just wrote in the doneBarButton is disabled as soon as the textFields are empty. It doesn't work that way. Do you have a solution? Or maybe a better way to solve it?

Was it helpful?

Solution

Either just connect value changed in interfacebuilder to a IBAction method in any of the classes you have in your view. Or you can do it in code with:

[textField addTarget:self 
              action:@selector(myIBActionMethod:) 
    forControlEvents:UIControlEventEditingChanged];

And check the length of the input.

You can of hook up both textfields to the same method and check the length of both textfields every time its called if you have IBOutlets to them both.

OTHER TIPS

I'd keep a reference for both UITextViews, lets say.-

IBOutlet UITextView *textView1;
IBOutlet UITextView *textView2;

properly linked to your xib/storyboards. Also, I'd rather use

- (void)textViewDidChange:(UITextView *)textView

callback. According to shouldChangeCharactersInRange docs, it looks like this method is called before actually changing the text.

As for the enabled condition, it would look something like this.-

self.doneBarButton.enabled = [textView1.text length] > 0 && [textView2.text length] > 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top