簡體   English   中英

(OSX / Cocoa)如何為主窗口設置控制器

[英](OSX/Cocoa) How to set controller for main window

我使用.xib文件創建了一個新的cocoa應用程序(不是故事板,應用程序必須向后兼容小牛/山獅)我希望有一個自定義窗口控制器用於主窗口。 這可能嗎? 我似乎無法找到一種方法將窗口連接到我想要的自定義控制器。 該窗口在AppDelegate中有一個參考插座,但是我需要一個自定義的NSWindowController用於此窗口,因為它在應用程序啟動時不會打開。 該應用程序以菜單欄應用程序靜默啟動,主應用程序通過菜單欄下拉按鈕啟動。

有沒有辦法將控制器鏈接到界面構建器中的窗口? 或者我必須做一些事情:

 wc = [[CustomWindowController alloc] initWithWindowNibName:@"Main"];

謝謝!

是的,在Interface Builder中打開Utilities (右側面板),然后在底部單擊Object Library (帶有方形的圓圈)。
搜索Object (藍色立方體),並將其拖到Document Outline (界面構建器左側的面板)中
從那里,選擇剛剛創建的對象,並將Identity InspectorClass更改為所需的窗口控制器。

最后,您可以進入Connections Inspector並將window連接到window插座

我似乎無法找到一種方法將窗口連接到我想要的自定義控制器。 該窗口在AppDelegate中有一個參考插座,但是我需要一個自定義的NSWindowController用於此窗口,因為它在應用程序啟動時不會打開。

其他方式:

1)刪除MainMenu.xib中的窗口。 刪除AppDelegate.m中的窗口屬性 - 因為您刪除了窗口,它不再相關。

2)文件>新建>文件>可可類。 輸入一個類名,例如MainWindowController; 選擇“子類:NSWindowController”; 選中“還為用戶界面創建.xib文件”。

3)在AppDelegate.m中創建一個插座:

#import "AppDelegate.h"
#import "MainWindowController.h"

@interface AppDelegate ()

@property (strong, nonatomic) MainWindowController* windowController;

@end

4)在AppDelegate.h中聲明一個動作:

@interface AppDelegate : NSObject <NSApplicationDelegate>

-(IBAction)launchWindow:(id)sender;

@end

並在AppDelegate.m中實現它:

- (void)launchWindow:(id)sender {
    [self setWindowController:[[MainWindowController alloc]
                               initWithWindowNibName:@"MainWindowController"]];

    [[self windowController] showWindow:nil];
}

5)在MainMenu.xib中,將菜單項連接到launchWindow()動作:控制從菜單項拖動到AppDelegate對象並選擇launchWindow。

創建控制器並使其從NSWindowController擴展。 在xib文件中,選擇文件所有者並將其設置為自定義類。 選擇您的NSWindow並將其連接到文件所有者。

要打開窗口:

在你的.h:

@property (strong, nonatomic) YourWindowController *yourWinController;

在你的.m:

self.yourWinController = [[YourWindowController alloc] initWithWindowNibName:@"YourWindowController"];
[self.yourWinController showWindow: nil];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM