1

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

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

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

// 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”

至少我不想使用不推荐使用的方法。但也许有更好的方法来处理整个事情?

4

0 回答 0