如图,我在MainMenu.xib中拖入了一个Object,设定成我自己的mainMenuWindowController类,然后把界面元素的Outlet和Action绑定到里面。
程序运行的时候会加载这个nib,然后实例化里面的对象
现在我想在其他的类里面使用这个对象,我该如何从nib里获取它呢?
ringa_lee2017-04-22 09:02:17
NSViewController* viewController = [[NSViewController alloc] initWithNibName:@"SomeNibName" bundle:nil];
高洛峰2017-04-22 09:02:17
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.