简体   繁体   中英

First Responder Explanation Needed

I asked a question earlier on here regarding the use of First Responder - and got a response here:

Trouble with First Responder

Would anyone mind giving me a 'dummies' version of this? Being new to Cocoa, I don't really know where to start with either one of these methods. I award answers quickly

Zach

First Responder is specifically this .

What you're asking about, though, is target-action. You have a UI object (button, menu item) that you need to cause multiple things to happen, but the UI object only sends one action.

Hence the solution: Make that action do multiple things.

Hook the UI object up to an action method you implement in your controller object (in your case, the document). In that method, do all the things the button needs to cause.

The subclassing solution is basically the same thing, except instead of hooking the UI object up to your document, you hook it up to the font manager, but you also make the font manager an instance of a subclass of NSFontManager that you create, rather than an instance of NSFontManager directly. In your subclass, you override addFontTrait: and add the other behavior in your implementation. At either the start or the end of that method, you send [super addFontTrait:sender] to invoke NSFontManager's implementation, so the original implementation gets done.

Long paragraph, but it's not actually all that much more work: The difference is just making the subclass and making the instance an instance of that subclass.


You've said before that “the Apple Documentation is incredibly vague”, but it's really not. There just happens to be a lot of it, and maybe you haven't been looking at the right documents.

These are the documents you need to read, from start to finish, and in order:

EDIT: This list is for Xcode 3. I posted an updated (for Xcode 4) version of this list in another answer .

  1. The Objective-C Programming Language
  2. The Memory Management Programming Guide for Cocoa
  3. The Cocoa Fundamentals Guide (which explains target-action, among other things)
  4. Application Architecture Overview
  5. Resource Programming Guide
  6. Interface Builder User Guide
  7. The Xcode 3 guides:
    1. Xcode Project Management Guide
    2. Xcode Workspace Guide
    3. Xcode Build System Guide
    4. Xcode Debugging Guide
  8. Document-Based Applications Overview

There is also an Instruments User Guide , but, unfortunately, that one is vague—or, to be more precise, incomplete. It omits a lot of useful information, like how to use Instruments' Zombies template to debug crashes. It's a high-level overview, nothing more.

Also, bookmark these:

That's a lot of reading, but it'll tell you everything you need to know, and that order is roughly the order you'll need to know it in.

The other answer says that you have two options:

First: Do replace the action by the one created by you and then implement the feature of the original version yourself. In this case, it suffices to just call the appropriate method of NSFontManager . That is, you add the original functionality to your own implementation of the method. This way, both actions are executed.

Second: Subclass the class where the original functionality is implemented and add your implementation by overriding the method that is called -addFontTrait . This way, your code gets executed "alongside". This question might help you to find the correct implementation.

So, the essence is that you can either add the original functionality to your implementation or the other way around. In this case, I would try the first one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM