Disable button if textField is empty

126 views
Skip to first unread message

Lucas Neiva

unread,
Sep 10, 2009, 4:49:26 AM9/10/09
to iphonesdkd...@googlegroups.com
Hello Guys,

I've got a textField in which the user is suposed to enter a name. I
can't accept an "empty" name.

So when the "done" Button got pressed and the textField was empty I
was just not dismissing the modal view in which the textField is in.
But I would much rather like to disable the button until I have a
String inside the textField.

I have tried a couple of things including:

- KVO, didn't work because the property "text" doesn't get changed
after every change in the textField

- UITextFieldDelegate, I couldn't find any appropriate method:
These have something to do with ResignFirstResponder(Which I never
do, because I only have one textField):
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
This one doesn't do it because I receive a call BEFORE the text has
changed:
– textField:shouldChangeCharactersInRange:replacementString:
optional method

Any ideas how I could do that?

Lucas Neiva

unread,
Sep 11, 2009, 6:26:29 AM9/11/09
to iphonesdkd...@googlegroups.com
No one? I simply want to do the same thing as for example in apple's contacts app in wich you can only save a new contact once you've entered a name or a phone number.

David Phillip Oster

unread,
Sep 11, 2009, 7:24:34 PM9/11/09
to iPhone SDK Development
Consider getting over it. Suppose the user gets to this modal view,
and changes his mind about it. In your plan, he'd have no way to
cancel.

David Phillip Oster

unread,
Sep 11, 2009, 7:27:27 PM9/11/09
to iPhone SDK Development
- (void)viewDidLoad {
[super viewDidLoad];
...
[name addTarget:self
action:@selector(nameChanged)
forControlEvents:UIControlEventEditingChanged];
}

- (void) nameChanged {
[myButton setEnabled:0 < [[name text] length]];
}

Lucas Neiva

unread,
Sep 12, 2009, 8:51:15 AM9/12/09
to iphonesdkd...@googlegroups.com
Dude, there is always the Cancel button...

Lucas Neiva

unread,
Sep 12, 2009, 8:52:56 AM9/12/09
to iphonesdkd...@googlegroups.com
So there is an event for that. Thank you very much!
Reply all
Reply to author
Forward
0 new messages