29

普段は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 を使用しています。

4

4 に答える 4

1

スウィフト 4:

// File main.swift
autoreleasepool {
   // Even if we loading application manually we need to setup `Info.plist` key:
   // <key>NSPrincipalClass</key>
   // <string>NSApplication</string>
   // Otherwise Application will be loaded in `low resolution` mode.
   let app = Application.shared
   app.setActivationPolicy(.regular)
   app.run()
}

// File Application.swift
public class Application: NSApplication {

   private lazy var mainWindowController = MainWindowController()
   private lazy var mainAppMenu = MainMenu()

   override init() {
      super.init()
      delegate = self
      mainMenu = mainAppMenu
   }

   public required init?(coder: NSCoder) {
      super.init(coder: coder) // This will newer called.
   }

}

extension Application: NSApplicationDelegate {

   public func applicationDidFinishLaunching(_ aNotification: Notification) {
      mainWindowController.showWindow(nil)
   }
}
于 2017-11-30T15:04:04.953 に答える
0

-initクラスのメソッドをオーバーライドしてウィンドウを作成し、そのウィンドウでのメソッド (の指定された初期化子) をAppWindowController呼び出します。super-initWithWindow:NSWindowController

しかし、NIB を避ける理由はほとんどないというコメントには概ね同意します。

于 2013-03-29T01:02:29.373 に答える