如圖,我在MainMenu.xib中拖入了一個Object,設定成我自己的mainMenuWindowController類,然后把界面元素的Outlet和Action綁定到里面。
程序運行的時候會加載這個nib,然后實例化里面的對象
現(xiàn)在我想在其他的類里面使用這個對象,我該如何從nib里獲取它呢?
歡迎選擇我的課程,讓我們一起見證您的進步~~
NSViewController* viewController = [[NSViewController alloc] initWithNibName:@"SomeNibName" bundle:nil];
MainMenu.xib generally has an instance of AppDelegate. First declare an IBOutlet of MainWindowController in the header file of the automatically generated AppDelegate class, then go back to MainMenu.xib and connect the instance of MainWindowController (blue cube) to the IBOutlet just declared in AppDelegate.
Then, the rest of the code needs to access the instantiated MainWindowController, which can be done through the IBOutlet property or ivar of AppDelegate. Of course, access will not be effective until after -awakeFromNib.