1

Is there a way to catch a "valueDidChange"-Event for a Textfield? oO

I've got a modalView with an UITextField.

When the UITextField is empty, the "Done"-Button in the NavigationBar should be disabled and when there is Text entered, it should become enabled.

Right now the only way to accomplish this by using the "textFieldShouldReturn"-Method. This works but is simply horrible for usability.

Isn't there a way to check the requirements every time a letter is being entered or removed?

4

3 回答 3

2

Implement -textField:shouldChangeCharactersInRange:replacementString:.

于 2010-04-04T14:45:22.423 回答
2

If anybody cares: the solution is to catch the control event "EditingChanged" with addTarget:selector:controlevent: ;)

于 2010-04-09T19:40:30.357 回答
2

I have a UITableViewController with self.editButtonItem which is enabled only if the UITextField.text has text in it. It's a way of ensuring that the user doesn't press "Done" before he/she has filled this UITextField with something. I think this is exactly what you're looking for. Here's how I did it:

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

    // Logs for seeing patterns, if you so please
    NSLog(@"replacementString length = %d",string.length);
    NSLog(@"textField.text = %@",textField.text);
    NSLog(@"range = (%d,%d)\n\n",range.location,range.length);

    if (string.length) {
        self.editButtonItem.enabled = YES;
    } else if (!string.length && textField.text.length == range.length) {
        self.editButtonItem.enabled = NO;
    }
    return YES;
}

I hope that helps

于 2012-03-21T06:02:49.190 回答