I am setting my first steps in OSX development and I’ve run into some problems. I have quite some experience with iOS development but the window system for OSX programs is something else.
I am making a client for a social network like twitter and need 2 seperate window controller for first starting the app, one if you are logged in to show your timeline and one for logging in, if you are not yet logged in. In the info.plist you need to give it a main.xib. For this I made an empty xib which I hide, the second the app starts. This is not really a good solutions IMO, what is a better solution for this? I want to keep the windows seperate from the appdelegate because that way I can keep my code seperated.
This gives me a problem, when I open my ‘second’ window to login it shows up but isn’t active. I have tried all the things like, orderFront:, activateIgnoringOtherApps:, makeKeyAndOrderFront: & more. But this all doesn’t work..
So: First off, is there a better way to handle the main.xib that is needed in the info.plist and if not, is there a way around the focus problem?
I’m working om osx 10.7
For more than one-offs, you really ought to separate your app delegate from your window controllers. Go ahead and create a new Cocoa application from the template. In
MainMenu.xib, delete the window. InAppDelegate.hdelete theIBOutletto theNSWindow. Create a couple new subclasses ofNSWindowControllercomplete withXIBs–perhapsLoginWindowControllerandTimelineWindowController.For “final”
NSWindowControllersubclasses (i.e. those which won’t be subclassed), the best practice for designated initializers isNow in your app delegate, you should have
@propertiesfor the two different window controller instances:You ought to have some sort of method in your app delegate that presents the correct window controller. Let’s call it
-updateWindowVisibility:This method as implemented above does a tiny bit more work than is necessary given the present setup, but should be easier to modify when you need to show/hide other windows. All that’s left to do at this point is to call
-updateWindowVisibilityfrom-applicationDidFinishLaunching:.I’ve posted an example app to github which demonstrates this approach.