• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to detect focus on an NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >How to detect focus on an NSTextField (From: Adam Gerson <email@hidden>)

  • Prev by Date: How can I filter out files in root directory like the Finder does?
  • Next by Date: Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
  • Previous by thread: Re: How to detect focus on an NSTextField
  • Next by thread: Re: How to detect focus on an NSTextField
  • Index(es):
    • Date
    • Thread