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 = m...@tidbits.com, 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to