How do I create a Cocoa window programmatically?

前端 未结 5 963
执念已碎
执念已碎 2020-12-04 08:21

My Cocoa app needs some small dynamically generated windows. How can I programmatically create Cocoa windows at runtime?

This is my non-working attempt so far. I see

5条回答
  •  天命终不由人
    2020-12-04 09:12

    A side note, if you want to programatically instantiate the application without a main nib, in the main.m file / you can instantiate the AppDelegate as below. Then in your apps Supporting Files / YourApp.plist Main nib base file / MainWindow.xib delete this entry. Then use Jason Coco's approach to attach the window in your AppDelegates init method.

    #import "AppDelegate.h":
    
    int main(int argc, char *argv[])
    {
    
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      [NSApplication sharedApplication];
    
      AppDelegate *appDelegate = [[AppDelegate alloc] init];
      [NSApp setDelegate:appDelegate];
      [NSApp run];
      [pool release];
      return 0;
    }
    

提交回复
热议问题