Question

I have been working on an app for MacOs. Their is a window in MainMenu.xib that I have put a set of views onto. Everything works great. Now, I find it would be useful to be able to File->New and have a new (second) blank copy of that window. I tried to move the connections to the window from AppDelegate.m to a new MainView.m object and have File->New call [[aMainView window] makeKeyAndFront] but it doesn't seem to do anything. I still have only one copy of the window.

Any advice is greatly appreciated!

Was it helpful?

Solution

Like hussain Shabbir correctly noted, makeKeyAndOrderFront: only makes an existing window key and orders it front; it does not create a new window.

The first thing you should do is rename “MainView” to something that more accurately describes what kind of thing it is. It certainly shouldn't be a view. While I'm on the subject, “main” means something specific in the context of an application's windows—that meaning is at odds with your “main” window, which may or may not be main, especially if the user can have more than one of them.

I recommend replacing MainView outright, with an NSWindowController. Conveniently, if you create a subclass of NSWindowController, Xcode will give you an option to create a nib along with it. That nib will have a window in it, which each window controller will load when needed.

Then, have your New menu item create a fresh window controller, throw it on the pile (you'll need to keep a mutable array of window controllers somewhere to keep them all alive and their windows visible, especially under ARC), and tell the window controller to show its window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top