PDA

View Full Version : keeping track of the urrent first responder




Mark FX
Feb 6, 2013, 02:01 PM
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
Feb 6, 2013, 02:19 PM
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/mac/#documentation/cocoa/conceptual/EventOverview/EventHandlingBasics/EventHandlingBasics.html

Mark FX
Feb 6, 2013, 02:27 PM
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
Feb 6, 2013, 02:39 PM
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
Feb 6, 2013, 02:42 PM
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
Feb 6, 2013, 02:56 PM
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
Feb 6, 2013, 03:52 PM
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.


[[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
Feb 6, 2013, 03:57 PM
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/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSControl_Class/Reference/Reference.html%23//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
Feb 8, 2013, 12:27 PM
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
Feb 8, 2013, 03:03 PM
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
Feb 8, 2013, 03:36 PM
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