Re: Setting a delegate on a UITextField
Re: Setting a delegate on a UITextField
- Subject: Re: Setting a delegate on a UITextField
- From: Jon Sigman <email@hidden>
- Date: Mon, 07 Mar 2011 15:11:48 -0800 (PST)
Matt, you are quite correct. It helps to simplify the case, and then build on
that foundation. I had too many things going on and couldn't see the forest for
the trees.
Thanks!
________________________________
From: Matt Neuburg <email@hidden>
To: Jon Sigman <email@hidden>
Cc: email@hidden
Sent: Mon, March 7, 2011 11:04:25 AM
Subject: Re: Setting a delegate on a UITextField
On Mar 7, 2011, at 9:54 AM, Jon Sigman wrote:
> On Mon, March 7, 2011 9:30:05 AM Matt Neuburg wrote:
>
> > The "Return" key invokes textFieldShouldReturn: and does *not* automatically
>resign first responder ...
>
> Ah! That is the part I had been overlooking. textFieldShouldReturn is the
>perfect place to validate the input.
>
> However, now that I've implemented it, textFieldShouldReturn gets called twice
>immediately when the "Return" key gets pressed on the keypad, not sure why...
> Thanks!
>
Once again I repeat my advice - start with a totally clean project with nothing
but a text field and its delegate and watch the delegate messages. Keep it
*simple* when exploring the framework. You will see that what you're saying is
false. The sequence will be:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%@", NSStringFromSelector(_cmd));
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(@"%@", NSStringFromSelector(_cmd));
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"%@", NSStringFromSelector(_cmd));
}
2011-03-07 11:02:00.934 Crud[7945:207] textFieldShouldReturn:
2011-03-07 11:02:00.936 Crud[7945:207] textFieldShouldEndEditing:
2011-03-07 11:02:00.938 Crud[7945:207] textFieldDidEndEditing:
Now add your validation.
I really can't advise using textFieldShouldReturn: for validation, because there
are many *other* ways in which a text field might resign first responder, and
then you'd miss out on your validation test. It is best to adopt habits that
work in general, with the intent of the framework, rather than just going with
something that happens to seem to work okay in one limited case. m.
--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pf
Programming iOS 4! http://www.apeth.net/matt/default.html#iosbook
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden