Re: simulating <enter> in a text field by pressing a button
Re: simulating <enter> in a text field by pressing a button
- Subject: Re: simulating <enter> in a text field by pressing a button
- From: "email@hidden" <email@hidden>
- Date: Mon, 07 Jan 2013 21:40:00 +0000
On 2 Jan 2013, at 21:03, Joel Reymont <email@hidden> wrote:
> I have a dialog (sheet) with a single text field and a button.
>
> I'm using Cocoa Bindings to validate the value in the text field and
> set a string field in the File's Owner.
>
> This works fine but I would like to trigger the same sequence of
> events when pressing the button. How do I do this?
Try calling -commitEditing on your object controller.
I also on occasion make use of the following NSWindow category method.
Most controls perform validation etc when they loose first responder status.
This method causes validation to occur by temporarily making the window its own responder.
/*
end all editing in window
*/
- (BOOL)endEditing:(BOOL)force
{
id firstResponder = [self firstResponder];
// gracefully end all editing in a window named aWindow
if ([self makeFirstResponder:self]) {
// All editing is now ended and delegate messages sent etc.
} else if (force) {
// For some reason the text object being edited will not resign
// first responder status so force an end to editing anyway.
// This is probably a bad idea as the first responder is probably refusing to resign
// its status because of a pending or failed validation.
// forcing the edit can leave our model in an invalid state.
[self endEditingFor:nil];
} else {
return NO;
}
// restore the first responder once editing completed.
// this is required in situations where this message is sent from
// - (void)windowDidResignKey:(NSNotification *)notification.
// a panel, such as the find panel, may be being displayed.
// it will require the window's firstResponder to be maintained.
[self makeFirstResponder:firstResponder];
return YES;
}
Regards
Jonathan Mitchell
Mugginsoft LLP
_______________________________________________
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