search

Home  >  Q&A  >  body text

objective-c - Cocoa开发中怎样获得实例化的ViewController类?

如图,我在MainMenu.xib中拖入了一个Object,设定成我自己的mainMenuWindowController类,然后把界面元素的Outlet和Action绑定到里面。
程序运行的时候会加载这个nib,然后实例化里面的对象
现在我想在其他的类里面使用这个对象,我该如何从nib里获取它呢?

天蓬老师天蓬老师2768 days ago359

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-04-22 09:02:17

    NSViewController* viewController = [[NSViewController alloc] initWithNibName:@"SomeNibName" bundle:nil];

    reply
    0
  • 高洛峰

    高洛峰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.

    reply
    0
  • Cancelreply