2

I've been searching for quite a while now but simply can't find anything, I already have window which I can create, but now when I tried to call makeKeyAndOrderFront for it, there was no application to call it for, so I was wondering if anyone could give a quick example on how do I create an application? I found out that it's NSApplication and I need to setDelegate etc, but not sure what exactly is required. for example, to create a window, I would've needed this code:

NSWindow *window;
window = [[NSWindow alloc] initWithContentRect: NSMakeRect(10, 100, 150, 200)
    styleMask:( NSResizableWindowMask | NSClosableWindowMask | NSTitledWindowMask)
    backing: NSBackingStoreBuffered
    defer: NO];
[window setTitle: @"Test window"];

So can anyone give me related code, but for NSApplication, I already have buttons and window?

  • Jeff has a series of blog posts about Cocoa applications without nib files, starting [here](http://lapcatsoftware.com/blog/2007/05/16/working-without-a-nib-part-1/). –  Jun 18 '11 at 08:47
  • This might be helpful if you're looking for a programmatical way to do this: http://stackoverflow.com/questions/2997333/creating-a-cocoa-application-without-nib-files/26322464#26322464 – eonil Oct 12 '14 at 07:11

2 Answers2

4

To do this entirely in code, with no nib at all, you first should remove the NSMainMenuNib entry from your Info.plist file. Then, in main:

  1. Create an autorelease pool.
  2. Ask the NSApplication class for the shared application object. If nothing has ever asked for the shared application object before, this will create one.
  3. Create an NSMenu to hold the main menu. Create a menu item for each of the standard menus, then create a menu for each of the standard menus, and set each menu as the menu item's submenu. For each menu, create the items that should go in it. Don't forget to localize titles and to set appropriate key equivalents. Set the top-level menu as the application's main menu.
  4. For an application with a single window, create the window at this point, create and give it its content view, create any views you want in it and add them to the content view, and order the window in.
  5. Tell the application to run.
  6. Drain the pool.
  7. Exit successfully.

That's the bare minimum. Setting a delegate is, as usual, optional.

When you create an application the usual way, all of this—except creating and adding subviews, which you'd do in IB—is done for you.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
3

Just start from one of the template available in Xcode. Then NSApplication is automatically set up.

If you want to do in the hard way, by coding everything, this series of blog posts "Working Without Nibs" will help you, which starts here. But you don't want to do that unless you have a good grasp of the inner workings of Cocoa.

So, just stick to what people usually do, and start from the template available in Xcode.

Yuji
  • 34,103
  • 3
  • 70
  • 88
  • Thanks for the link, rest was waste of time, if I wanted to do it like "people usually do", I would've done it that way (: –  Jun 17 '11 at 15:51
  • 6
    @Mahi: then you could have started your question by saying "I know how to do this using nib files, but for curiosity I want to know ... " or something like that:p – Yuji Jun 17 '11 at 15:55