鍍金池/ 問答/C  iOS  網(wǎng)絡(luò)安全  HTML/ 怎樣使用純代碼方式搭建macOS App UI界面?

怎樣使用純代碼方式搭建macOS App UI界面?

幾乎所有的示例或開源項(xiàng)目均使用xib,現(xiàn)在我需要通過純代碼方式使用NSWindowController 、NSWindowNSViewController 三個類搭建UI界面。

通過如下代碼能夠成功的顯示界面,但我個人感覺這似乎不是正確的創(chuàng)建方式。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application

MainViewController *mainVC = [[MainViewController alloc] init];
mainVC.title = @"CocoaDemo";

MainWindow *mainWindow = [MainWindow windowWithContentViewController:mainVC];
[mainWindow makeKeyAndOrderFront:nil];
[mainWindow center];

// Create a window controller
MainWindowController *mainWC = [[MainWindowController alloc] initWithWindow:mainWindow];
// Put the window of the window controller on screen
[mainWC showWindow:self];
self.mainWC = mainWC;
}

而且通過上述方式創(chuàng)建UI至少存在以下問題:

  1. NSWindowControllerloadWindow、windowDidLoad、windowWillLoad方法均不會被調(diào)用
  2. 封裝性差

NSWindowControllerloadWindow、windowDidLoad、windowWillLoad方法的官方文檔描述如下:

- loadWindow Loads the receiver’s window from the nib file.
You should never directly invoke this method. Instead, access the window property so the windowDidLoad and windowWillLoad methods are invoked. Subclasses can override this method if the way it finds and loads the window is not adequate. It uses the NSBundle class’s bundleForClass: method to get the bundle, using the class of the nib file owner as argument. It then locates the nib file within the bundle and, if successful, loads it; if unsuccessful, it tries to find the nib file in the main bundle.

- windowDidLoad Sent after the window owned by the receiver has been loaded.
The default implementation does nothing.

- windowWillLoad Sent before the window owned by the receiver is loaded.
The default implementation does nothing.

根據(jù)字面理解這三個方法作用也不太明確,請各位大神傳授正確地純代碼創(chuàng)建macOS App方式。

如若有任何的想法或建議請您不吝筆墨,本人將不勝感激,在此先謝謝各位!

回答
編輯回答
不討囍

我也想知道怎么實(shí)現(xiàn),同樣遇到這個問題。

2017年10月26日 10:28