How can “First Responder” be used in a xib file?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 17:25:57

问题


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 ?!

Thanks in advance.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4652721/how-can-first-responder-be-used-in-a-xib-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!