Re: How to detect focus on an NSTextField
Re: How to detect focus on an NSTextField
- Subject: Re: How to detect focus on an NSTextField
- From: Bill Garrison <email@hidden>
- Date: Fri, 8 Feb 2008 12:29:45 -0500
- Resent-date: Fri, 8 Feb 2008 12:31:42 -0500
- Resent-from: Bill Garrison <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: email@hidden
On Feb 8, 2008, at 2:32 AM, Adam Gerson wrote:
I have done some exhaustive searching on google to find a way to
detect if my NSTextField has focus (if the user has click in it or
has typed something in it). The best I could find is:
[urlEntryTextField currentEditor];
Which does not appear to work 100% of the time. I have an editable
NSTableView that doesn't want to give up focus under certain
conditions when I click out of it and into the NSTextView.
I found alot of convoluted posts. Is there an simple way to detect
if a control has current focus?
Adam,
If you need to detect when a particular control has become first
responder, you could subclass the control and override -
becomeFirstResponder to signal your application in some way that the
control has just gotten focus.
There is the existing -controlTextDidBeginEditing delegate, but it
doesn't fire until the user acts on the field after it has obtained
focus.
I just did this last night for an NSSecureTextField. My subclass
overrides -becomeFirstResponder to invoke an informally defined
delegate method.
I needed to detect when the text field first got focus so that I could
enable a window controller to populate the text field's content from
an item in the user's keychain. In IB, I changed the class of the
secure text field to my subclass and made the window controller its
delegate. In my window controller, I then implemented the custom
control's new custom delegate method, -
controlDidBecomeFirstResponder. As soon as the secure text field got
focus, the custom -controlDidBecomeFirstResponder: method fired and I
was able to respond accordingly.
Maybe something similar would work for you.
Bill
MySecureTextField.m
------------------------------
#import "MySecureTextField.h"
#import "NSControl+FirstResponderNotification.h" // defines the custom
notification and a corresponding informal delegate method
@implementation MySecureTextField
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (BOOL)becomeFirstResponder;
{
// If the control's delegate responds to
controlDidBecomeFirstResponder, invoke it. Also post a notification.
BOOL didBecomeFirstResponder = [super becomeFirstResponder];
NSNotification *notification = [NSNotification
notificationWithName:MyControlDidBecomeFirstResponderNotification
object:self];
if ( [self delegate] && [[self delegate]
respondsToSelector:@selector(controlDidBecomeFirstResponder:)] ) {
[[self delegate] controlDidBecomeFirstResponder:notification];
}
[[NSNotificationCenter defaultCenter] postNotification:notification];
return didBecomeFirstResponder;
}
@end
NSControl+FirstResponderNotification.h
--------------------------------------------------------------------------------
extern NSString * const MyControlDidBecomeFirstResponderNotification;
@interface NSControl (FirstResponderNotification)
/*!
@method controlDidBecomeFirstResponder
@abstract An informal protocol method to be invoked by NSControl
delegates when the receiver becomes first responder.
@param notification A MyControlDidBecomeFirstResponderNotification.
The notification object is the control that became first responder.
There is no userInfo.
*/
- (void) controlDidBecomeFirstResponder:(NSNotification *)aNotification;
@end
_______________________________________________
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