简体   繁体   English

需要第一响应者说明

[英]First Responder Explanation Needed

I asked a question earlier on here regarding the use of First Responder - and got a response here: 我之前在这里提出了一个关于使用First Responder的问题 - 并在此得到了回复:

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. 作为Cocoa的新手,我真的不知道从哪种方法开始。 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. 您有一个UI对象(按钮,菜单项),您需要导致多个事情发生,但UI对象只发送一个动作。

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). 将UI对象挂钩到您在控制器对象中实现的操作方法(在您的情况下,文档中)。 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. 子类化解决方案基本上是相同的,除了不将UI对象挂钩到您的文档,您将它连接到字体管理器,但您还使字体管理器成为您创建的NSFontManager的子类的实例,而不是一个NSFontManager的实例直接。 In your subclass, you override addFontTrait: and add the other behavior in your implementation. 在您的子类中,您覆盖addFontTrait:并在您的实现中添加其他行为。 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. 在该方法的开始或结束时,您发送[super addFontTrait:sender]以调用NSFontManager的实现,因此原始实现完成。

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 . 编辑:此列表适用于Xcode 3.我在另一个答案中发布了此列表的更新(对于Xcode 4)版本。

  1. The Objective-C Programming Language Objective-C编程语言
  2. The Memory Management Programming Guide for Cocoa 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 Interface Builder用户指南
  7. The Xcode 3 guides: Xcode 3指南:
    1. Xcode Project Management Guide Xcode项目管理指南
    2. Xcode Workspace Guide Xcode工作区指南
    3. Xcode Build System Guide Xcode构建系统指南
    4. Xcode Debugging Guide Xcode调试指南
  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. 它省略了许多有用的信息,比如如何使用Instruments的Zombies模板来调试崩溃。 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 . 在这种情况下,只需调用NSFontManager的相应方法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 . 第二:对实现原始功能的类进行子类化,并通过覆盖名为-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. 在这种情况下,我会尝试第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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