如何从辅助进程加载nib?

时间:2012-11-26 04:27:07

标签: objective-c macos cocoa

我有一个使用NSTask生成的需要显示窗口的进程。我开始只是手工编写所有的UI代码,但这已经成为一种痛苦。

所以,我创建了一个带有xib MyWindowController的新类。我想在辅助进程中加载​​这个控制器的一个实例,并让所有的IBOutlets和whatnot工作正常。

这是我到目前为止所得到的:

// Get the bundle for the main application (not the subprocess).The executable lives in Contents/Helpers, so look two dirs up from its path for the main app bundle root.
NSArray *executablePathComponents = [[[NSBundle mainBundle] executableURL] pathComponents];
NSIndexSet *indexOfEveryComponentExceptLastTwo = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [executablePathComponents count] - 2)];
NSBundle *myBundle = [NSBundle bundleWithURL:[NSURL fileURLWithPathComponents:[executablePathComponents objectsAtIndexes:indexOfEveryComponentExceptLastTwo]]];

// Load the controller nib.
NSNib *windowControllerNib = [[NSNib alloc] initWithNibNamed:@"MyWindowController" bundle:myBundle];
MyWindowController *windowController = [[MyWindowController alloc] init];
NSArray *topLevelObjects = nil;
[windowControllerNib instantiateNibWithOwner:windowController topLevelObjects:topLevelObjects];

这给了我一个窗口控制器的实例,它从屏幕上的笔尖显示窗口,所以这似乎有效。但是,不推荐instantiateNibWithOwner:topLevelObjects支持instantiateNibWithOwner:topLevelObjects。

使用不推荐使用的方法会导致异常:“ - [NSNib instantiateWithOwner:topLevelObjects:]:无法识别的选择器发送到实例0x10291ab1”

至少我不想使用弃用的方法。但也许有更好的方法来处理这整个问题?

0 个答案:

没有答案