16

i read about responder chains, and basically i read about how the events from the UI are passed up on the responder chain, how to handle them and how to pass along. I also used -becomeFirstResponder and -resignFirstResponder, but i was using these only for basic tasks, like showing keyboard on iOS and handling shake events.

What I would like to know is how can I use the "First Responder" proxy object in the MainWindow.xib that Xcode generates. I see some received actions and also i see that i can create actions on the Identity inspector tab.

So my question is: how can I use these actions, and when should I use this proxy object in IB ?!

Interface Builder Screenshot

Thanks in advance.

1
  • Actually what is that proxy object in the first place? A button? What?
    – user4951
    Nov 13, 2012 at 3:35

2 Answers 2

8

Assigning an action to the first responder object is the same as adding an action to a control with a nil target programatically. When the action is triggered, the application will go through the responder chain and send the action to the first responder that accepts it. On the mac, it is often used to automatically send menu commands to the current window or document. You should use it if you have multiple objects which you want to send messages to with the same button.

4
  • what is a control with nil target? I thought each control has many targets depending on the events.
    – user4951
    Nov 13, 2012 at 3:34
  • @JimThio You parsed that wrong. It was "adding a nil-targeted action to some control", not "adding some action to a nil-targeted control." The target is set per action, not per control. Nov 13, 2012 at 5:05
  • Actually that's true. Target is set per action for cocoa touch. It looks like firstresponder is useless for cocoa touch then.
    – user4951
    Nov 13, 2012 at 7:32
  • First responder is the target if you haven't designated a specific object. Most of the time you don't use it on iOS because you know who the first responder is going to be anyway (hello view controller). It's more likely to matter on iOS if you're making a library of reusable controls or something like that. Nov 13, 2012 at 20:50
3

You would use First Responder as the target of an action whenever you want the action to be sent to the first responder, regardless of which particular object that happens to be at the time the action is sent.

In desktop Cocoa, a message targeted for the First Responder is in fact a nil-targeted action - no target is implicitly understood as the first possible responder, the First Responder.

Often, textual actions are directed to the First Responder. You want keypresses to be sent to whichever text field has focus, but the text field that has focus changes over the life of the view and of the application. You could manually retarget the action each time a text field get focus, but thanks to NSResponder, you don't have to bother - that's all taken care of for you.

6
  • 1
    Thanks for the answer Jeremy. It seems on the iPhone there is no big use for this feature (or at least I can see no use so far) ...
    – Moszi
    Jan 11, 2011 at 11:53
  • What do you mean object that "happens to be"? Objects happens to be at the time the action is sent. There are many objects when an action is sent. Which one?
    – user4951
    Nov 13, 2012 at 3:36
  • @JimThio The first responder at the time the action is sent. There can be many objects, but there can be only one first responder in a window at any given time. Nov 13, 2012 at 5:04
  • So first responder is the first responder in the responder chain that actually respond to the event. If I have childView in parentView and childView.UserInteractionEnabled=false, then the parentView is the first responder. Am I correct here?
    – user4951
    Nov 13, 2012 at 7:33
  • First responder is the name for the "check here first" responder. If they don't handle it, it bubbles up through the responder chain. For details, see Apple's docs. Nov 13, 2012 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.