普段はiOSアプリを作っているのですが、今はOS Xアプリを作ろうとしていて、最初から迷っています。私がiOSアプリを作るスタイルは完全にプログラム的であり、xibファイルなどはありません. ただし、OS X プログラミングでは、メニュー項目とデフォルト ウィンドウを含むいくつかの xib ファイルから開始します。メニュー項目には非常に多くの項目があるため、おそらくいじりたくないのですが、プログラムで最初のウィンドウを自分で作成したいと考えています。
だから私はこれをしました:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSUInteger windowStyleMask = NSTitledWindowMask|NSResizableWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask;
NSWindow* appWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, 200, 1280, 720) styleMask:windowStyleMask backing:NSBackingStoreBuffered defer:NO];
appWindow.backgroundColor = [NSColor lightGrayColor];
appWindow.minSize = NSMakeSize(1280, 720);
appWindow.title = @"Sample Window";
[appWindow makeKeyAndOrderFront:self];
_appWindowController = [[AppWindowController alloc] initWithWindow:appWindow];
[_appWindowController showWindow:self];
}
ここでは、最初にウィンドウを作成し、その windowController を使用してこのウィンドウを初期化します。ウィンドウはこのように表示されますが、ここではボタンやラベルなどの内部要素のみを指定できますが、windowController では指定できません。気持ち悪いので別の方法でやってみました。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
_appWindowController = [[AppWindowController alloc] init];
[_appWindowController showWindow:self];
}
この後loadWindow:
、windowController の関数の他の要素を次のように設定します。
- (void)loadWindow
{
[self.window setFrame:NSMakeRect(200, 200, 1280, 720) display:YES];
self.window.title = @"Sample window";
self.window.backgroundColor = [NSColor lightGrayColor];
NSButton* sampleButton = [[NSButton alloc] initWithFrame:NSRectFromCGRect(CGRectMake(100, 100, 200, 23))];
sampleButton.title = @"Sample Button!";
[sampleButton setButtonType:NSMomentaryLightButton];
[sampleButton setBezelStyle:NSRoundedBezelStyle];
[self.window.contentView addSubview:sampleButton];
NSLog(@"Loaded window!");
[self.window makeKeyAndOrderFront:nil];
}
残念ながら、これはうまくいきません。loadWindow:
が呼び出されることはありませんwindowDidLoad:
。彼らはどこに行きましたか?
なぜ私がニブを使わないのか聞かないでください。おそらくOpenGLの内部で高度にカスタマイズされたビューを作成したいので、ペン先ではそれを処理できないと思います。誰かが助けていただければ幸いです。ありがとう。
また、プログラムでメニュー項目をゼロから開始する方法を誰が知っていますか?
最新の Xcode を使用しています。