ウィンドウを表示する必要がある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に送信されました」
少なくとも、非推奨のメソッドは使用したくありません。しかし、おそらくこのすべてにアプローチするためのより良い方法がありますか?