如何在XCode 4中使用NSDocumentController的子类? [英] How do I use a subclass of NSDocumentController in XCode 4?

查看:101
本文介绍了如何在XCode 4中使用NSDocumentController的子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试教自己可可开发的过程。为此,我购买了一本极好的书,Mac OS X的Cocoa Recipes:Vermont Recipes,它将逐步介绍如何创建示例应用程序。这是相当不错,但它是针对XCode 3.2而不是XCode 4.到目前为止,我已经能够桥接这个自己,但我遇到了一个问题,我不知道如何遵循指令。



实质上,本书通过子类化 NSDocumentController 的示例案例,以便应用程序可以处理两个任意数量)不同类型的文档,因此它为每种类型打开适当的窗口。所以,我创建了一个自定义子类 NSDocumentController (这本书叫 VRDocumentController ),现在我需要使得该控制器的实例在应用程序启动过程中相对较早地加载。基本上,类是单例,因此我必须在应用程序实例化标准类之前实例化我的,这必须在过程的早期完成。够了。



这本书引用了用于子类化NSDocumentController的Apple文档,它说明有两种方法来攻击问题:在 MainMenu.xib 文件或在您的 -applicationWillFinishLaunching:委托方法中实例化一个。苹果文档不会给出明确的指导,如何做这些(在一会儿更多),本书只涵盖第一个版本,我认为可能是我的首选方法。



我的问题:我不能为我的生活在XCode 4拉这个。这本书为XCode 3.2提供的说明已不再准确,因为Interface Builder已经洗牌到XCode本身,版本的类选项卡不再显示我的项目的类。我发现 Stack Overflow上的这个问题提出了类似的问题,所以我尝试按照接受的答案。然而,当我打开Identity Inspector并尝试键入 VRDocumentController 时,它只是嘟嘟在我身上,不采取它。我写的其他控制器类似乎也不是可以接受的输入。



我也很乐意去其他路线;在 -applicationWillFinishLaunching 方法中实例化一个副本。但是,我没有地球的想法,该方法实际属于哪个类,或者它的返回类型是什么。

解决方案

在您的应用程序委托中:我已经做了一个不小的搜索,

  // LukeAppDelegate.h 
#importLukeAppDelegate.h
#importVRDocumentController

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
VRDocumentController * dc = [[VRDocumentController alloc] init];
}

这将确保 VRDocumentController 创建并注册为共享文档控制器,防止Cocoa使用默认的 NSDocumentController



至于为什么您无法在nib文件中使用自定义对象,请确保在将新对象拖动到其中时选择对象(蓝色立方体)而不是对象控制器(绿色圆形内的蓝色立方体)

编辑:如果您定位的操作系统版本支持恢复, -applicationWillFinishLaunching:可能太晚,无法注册自定义文档控制器。如果应用程序委托放置在MainMenu.xib内部,应该在任何文档恢复之前由nib加载进程实例化,因此您可以将 NSDocumentController 子类初始化移动到应用程序委托的init方法:

  // LukeAppDelegate.h 
#importLukeAppDelegate.h
#import VRDocumentController

- (id)init {
self = [super init];
VRDocumentController * dc = [[VRDocumentController allocate] init];
return self;
}


I am currently in the process of trying to teach myself Cocoa development. Toward this end, I purchased a mostly-excellent book, Cocoa Recipes for Mac OS X: Vermont Recipes, which walks through creating a sample application. It's pretty good, but it's written against XCode 3.2 rather than XCode 4. So far, I've been able to bridge this myself, but I've run up against an issue where I can't figure out how to follow the instruction.

Essentially, the book goes through a sample case of subclassing NSDocumentController so that the application can handle two (eventually maybe an arbitrary number) different types of documents, and so it opens the appropriate window for each type. So, I've created a custom subclass of NSDocumentController (which the book calls VRDocumentController), and now I need to make it such that an instance of this controller loads relatively early in the application launch process. Basically, the class is a singleton, and so I have to instantiate mine before the application instantiates the standard class, and this has to be done early in the process. Fair enough.

The book cites the Apple documentation for subclassing NSDocumentController, which states that there are two ways to attack the problem: to instantiate the class in your MainMenu.xib file or to instantiate one in your -applicationWillFinishLaunching: delegate method. The Apple documentation doesm't give clear instruction on how to do either of these (more on that in a moment) and the book covers only the first version, which I think is probably my preferred method.

My problem: I cannot for the life of me pull this off in XCode 4. The instructions the book provides for XCode 3.2 are no longer accurate, because Interface Builder has been shuffled into XCode itself now and the new version of the "classes tab" no longer shows my project's classes. I found this question on Stack Overflow asking a similar question, so I tried following the accepted answer there. However, when I open the Identity Inspector and try to type VRDocumentController, it just beeps at me and doesn't take it. None of the other controller classes I've written seem to be acceptable inputs either.

I'd also be happy to go the other route; instantiating a copy in the -applicationWillFinishLaunching method. But, I have no earthly idea in which class that method actually belongs, or what its return type is. I've done a non-trivial amount of searching for that, too, with no luck.

解决方案

In your application delegate:

// LukeAppDelegate.h
#import "LukeAppDelegate.h"
#import "VRDocumentController"

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    VRDocumentController *dc = [[VRDocumentController alloc] init];
}

This will make sure that an instance of VRDocumentController is created and registered as the shared document controller, preventing Cocoa from using the default NSDocumentController.

As to why you haven’t been able to use a custom object in your nib file, make sure that that you select Object (blue cube) instead of Object Controller (blue cube inside a green sphere) when dragging a new object into the nib file.


Edit: If you’re targeting an OS X version that supports restoration, -applicationWillFinishLaunching: may be too late to register a custom document controller. If the application delegate is placed inside MainMenu.xib, it should be instantiated by the nib loading process before any documents are restored, hence you can move the NSDocumentController subclass initialisation to the application delegate’s init method:

// LukeAppDelegate.h
#import "LukeAppDelegate.h"
#import "VRDocumentController"

- (id)init {
    self = [super init];
    VRDocumentController *dc = [[VRDocumentController alloc] init];
    return self;
}

这篇关于如何在XCode 4中使用NSDocumentController的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆