Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

petron

macrumors member
Original poster
May 22, 2009
95
0
Malmo, Sweden
Hi,
I wonder if there is a way to simply find out if a specific NSTextField is active or in Focus.

Probably one can use the firstResponder... like this but I get warning
NSResponder may not respond to delegate...

Code:
NSTextField *aFiled = [thisWindow firstResponder] delegate]
if (aField == muTextField) {
    NSLog(@"muTextField is in focus");
} else {
   NSLog(@"muTextField is not in focus");
}


Are there any other means of getting this done ?

I will appreciate a tip.
Thank you in advance.
/petron
 
Just compare the firstResponder directly:
Code:
if ([thisWindow firstResponder] == muTextField) ...
 
Hi Kainjow

It does not help.

It never be equal, I have tested it already. I got it equal only if I refer to "delegate" as in my example,then it works but there is a warning.

Any clue why ?

/petron
 
Thanks for trying.
In NSDocument application.

I have several NSTextFields and depending on which one is active/in Focus a specified action is required when NSButton is pressed.

I got it working by doing this, but do not understand why.

Code:
    if ([(id)[thisWindow firstResponder] delegate] == (id)bookmarkField) {
        NSLog(@" field active make action A");
    } else {
        NSLog(@" field not active make action B");
    }

/petron
 
I believe a text field will send NSControl's delegate a -control:textShouldBeginEditing: whenever it gets focus (assuming it is enabled and editable). You could make your NSDocument object (or some other object) a delegate to the text fields and use that message to set the action on the button.
 
Hi Sydde,
Well it can be done, but I am not interested when it gets focus, I am interested if it has focus when I press a button or press enter.

I got it working when pressing button , but when pressing enter I do get different firstResponder. This bothers me.
What happen with responder when we press ENTER, Why it is not longer the highlighted NSTextField...

Lots of Questions..

Thanks any way.
/petron
 
For a NSTextField, enter is a signal that editing is done. Toucan also capture this with a delegate method, -control:shouldEndEditing: . This can be useful if you want the program to decide which field should get focus (e.g., if the content of the field might suggest targeting a different field). I have not studied up much on the responder chain, you might want to.
 
Oh I just realized... The first responder may be the field editor. you may want to read up on that too.
 
Thanks for comments I got it working in another way. No sides effects as when using the firstRespnder construction.

in the
- (void)awakeFromNib

I set up the target and action of the NSTextField

Code:
    [muTextField setTarget:self];
    [muTextField setAction:@selector(makeAnAction:)];

Then pressing ENTER the makeAnAction method is called.
This works fine for me.

/petron
 
To determine if NSTextField is in focus, you can use

Code:
- (BOOL)isTextFieldInFocus:(NSTextField *)textField
{
	BOOL inFocus = NO;
	
	inFocus = ([[[textField window] firstResponder] isKindOfClass:[NSTextView class]]
			   && [[textField window] fieldEditor:NO forObject:nil]!=nil
			   && [textField isEqualTo:(id)[(NSTextView *)[[textField window] firstResponder]delegate]]);
	
	return inFocus;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.