Reputation: 13
So here i was trying to get rid of @NSApplicationMain to get familiar on how things work more in depth. I found this nice answer from musically_ut here OSX application without storyboard or xib files using Swift
However, its not quite working ! Here's the weird result : the view is not linked to the window, furthermore, the window is not where it should be!
the image can be found here http://imgur.com/e4EXnpW
I started the xcode project without using storyboard, then created main.swift, AppDelegate.swift, ViewController.swift, and pasted each of the content the guy provided. However its not working.
Could someone kindly provide a reliable way to start an OSX App without @NSApplicationMain ?
Upvotes: 0
Views: 1018
Reputation: 13316
It looks like your code actually is working, but that there's a few other things you need to do to get it to work the way you want it to. You have one window that is being loaded from the MainMenu.xib
in Interface Builder - that is the window with "911" in the title bar. You've got the second window that you are creating programmatically. That is the window in the lower left corner of the screen.
First, it looks like you still have a Window
in your MainMenu.xib
file. Click on MainMenu.xib
, and when it opens in Interface Builder, look at the items on the left-hand pane. I'm guessing that there is a Window
icon. You want to click on that and delete it.
After that you will just have the single window in the lower-left hand corner. It looks like a "view" that is unattached to a window because you haven't configured the window's styleMask
property. You probably want a styleMask
that looks like this:
let styleMask = NSTitledWindowMask
| NSClosableWindowMask
| NSMiniaturizableWindowMask
| NSResizableWindowMask
You say that the window also isn't where it is supposed to be. I don't know where you want it to be, but you can change its position and size by changing the contentRect
parameter when you initialize it. Make a NSRect
that positions and sizes your window where you want it, call it something convenient like myRect
, and then initialize your window like this (this assumes you defined the styleMask
constant from above):
newWindow = NSWindow(contentRect: myRect, styleMask: styleMask, backing: NSBackingStoreType.Buffered, defer: false)
That should do it. For an alternative version of main.swift
, see this gist on github.
Upvotes: 2