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

Mark FX

macrumors regular
Original poster
Nov 18, 2011
159
17
West Sussex, UK
Currently I have a project with an AppController which has the standard MainMenu.xib, with main window and menu bar, I have added a NSViewController class to the project, which controls and displays a custom view class, which is a NSBox subclass, with a xib file that has the NSBox in Inteface Builder.

I have added several TextFields to the box in IB, the app controller gets the view controller to add the box view to the main window, as a subview, that all works as it should, and the text fields all recieve the focus and first responder status as they should with the tab key or mouse click.

My question is, how can I monitor which text field has the focus?

I have IBOutlets to all of the text fields in the NSBox subclass at the moment, and have used the NSResponder keyUp: method to check which text field has first responder status, but the mouse events dont work in the NSBox subclass as the text fields themselves recieve the mouse down and mouse up events, so can not check which text field has been clicked.

I'm sure there is a simple awnser I missing, but How can I check the active text field, when the field is clicked by the mouse, and not tabbed to by the keyboard?

Thanks in advance Mark
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
My question is, how can I monitor which text field has the focus?
...
I'm sure there is a simple awnser I missing, but How can I check the active text field, when the field is clicked by the mouse, and not tabbed to by the keyboard?

What are you trying to accomplish, or what problem are you trying to solve, by doing this?


Text field focus and first-responder status is specifically discussed under "Responder-Related Tasks" in this document:
http://developer.apple.com/library/.../EventHandlingBasics/EventHandlingBasics.html
 

Mark FX

macrumors regular
Original poster
Nov 18, 2011
159
17
West Sussex, UK
What are you trying to accomplish, or what problem are you trying to solve, by doing this?

As the question implies, I am trying to know the identity of the currently active text field, from many text fields in a custom view.

I could add an NSTextField sublclass to the project, and handle the keyboard or mouse events in this new subclass, but I was trying to find a solution using the NSBox subclass, or its view controller, but cannot find the awnser using these means.

Mark
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
You need your subclass to become a delegate of the text field and implement the control:textShouldBeginEditing: method or do whatever is appropriate.

Are you just trying to make a visual change or control/modify input? It sounds like visual changes. Are there not already sufficieny visual cues as part of the default text field controls or customization options in Interface Builder?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
As the question implies, I am trying to know the identity of the currently active text field, from many text fields in a custom view.

I know that. My question was why does knowing the identity of that text field matter? You believe that knowing this identity is important for some reason, and I'm asking what that reason is.

One can trigger custom actions on specific text fields by giving them a customized editor. There are also delegates that may come into play.

Without knowing what problem you're trying to solve, for which you think knowing the identity of the text field is crucial, we can't really suggest other ways to solve the problem, because we don't know what problem your proposed strategy (knowing the identity of the text field) is trying to solve.
 

Mark FX

macrumors regular
Original poster
Nov 18, 2011
159
17
West Sussex, UK
As originally quoted by chown33
Without knowing what problem you're trying to solve, for which you think knowing the identity of the text field is crucial, we can't really suggest other ways to solve the problem, because we don't know what problem your proposed strategy (knowing the identity of the text field) is trying to solve.

The reason for knowing the identity of the currently active text field is that the user will be able to add predefined custom text formatters to the input, and also the text will be later used in another data form, with its custom formatting being used in this other display.

Yes I have some options for getting the active text field, by subclassing NSTextField, and handeling the mouse and keyboard events in this subclass, and also I can ask the main key window for the current first responder, but I need to know when the text field has been clicked in order to ask the window for this info, But just thought there might be a way that I've missed, for asking the text field's superview, or superviews controller, but I'm starting to think this is'nt possable.

Regards Mark
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
4
Wilmington, DE
The reason for knowing the identity of the currently active text field is that the user will be able to add predefined custom text formatters to the input, and also the text will be later used in another data form, with its custom formatting being used in this other display.

Yes I have some options for getting the active text field, by subclassing NSTextField, and handeling the mouse and keyboard events in this subclass, and also I can ask the main key window for the current first responder, but I need to know when the text field has been clicked in order to ask the window for this info, But just thought there might be a way that I've missed, for asking the text field's superview, or superviews controller, but I'm starting to think this is'nt possable.

Regards Mark

You have the right idea about asking the key window for its first responder, and you can do it by listening for NSApplicationDidUpdateNotifications.

Code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTheFirstResponder:) name:NSApplicationDidUpdateNotification object:nil];

Then just implement - (void)getTheFirstResponder:(id)sender method to handle the notification. Note that this notification will be sent out after every single event, so its going to get posted often.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Yes I have some options for getting the active text field, by subclassing NSTextField, and handeling the mouse and keyboard events in this subclass, and also I can ask the main key window for the current first responder, but I need to know when the text field has been clicked in order to ask the window for this info, But just thought there might be a way that I've missed, for asking the text field's superview, or superviews controller, but I'm starting to think this is'nt possable.

As noted in Sayer's post, see the text-editing delegate methods. The API is in NSControl, not NSTextField. If you didn't see it, it may be because you didn't look in the superclasses of NSTextField.

http://developer.apple.com/library/...e/Reference.html#//apple_ref/occ/cl/NSControl

There is a delegate method for when editing begins, controlTextDidBeginEditing:, and another for when it ends, controlTextDidEndEditing:. There is a third method for any time it changes.

Multiple text fields can have the same delegate object. Refer to the method descriptions in the linked reference to see what is passed in the userInfo dictionary of the notification.
 

Mark FX

macrumors regular
Original poster
Nov 18, 2011
159
17
West Sussex, UK
Sorry for the slow reply, but thanks to you all for your advice, I'm not going to go for the NSTexFields edit tracking methods, as they are overkill for simply checking the first responder, but I like the idea that ElectricSheep has pointed at with the notification method, Thanks!, but I will check to see how busy that method is with a log statement.

As stated previously I only need to check wether the mouse clicks into the text field, as I already have a way of checking keyboard events for the first responder, with the keyUp method in he textfield's superview.

I think I have grasp of the options now, so again thanks everyone.

Regards Mark
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Haven't done it in Cocoa but in CocoaTouch you can just write a category for UIView that walks through the view hierarchy until it finds the first responder, then returns it.
 

Mark FX

macrumors regular
Original poster
Nov 18, 2011
159
17
West Sussex, UK
Thanks Jared Kipe that might me worth looking into, if it can be done for iOS, I'm sure it could be achieved in an OSX project.

Regards Mark
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.