0

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!

Bug
  • 2,576
  • 2
  • 21
  • 36
  • Why you are using this([[aMainView window]makeKeyAndOrderFront:self];), This will only make your order of winodow in the first order?? – Hussain Shabbir Oct 03 '13 at 05:39

1 Answers1

1

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I see. In this case, MainView is a NSWindowController, just badly named out of laziness, I suppose. I was really hoping not to have to move my already designed window into a new nib file. I get the impression that I will have to do so, however. Thanks! – Peter Schury Oct 04 '13 at 00:43
  • I have done as you suggest. I can now create several instances of my new NSWindowController. I can call methods from them. However, I cannot get the window to display. When I query the NSWindowController for the window title it responds (null). help? – Peter Schury Oct 04 '13 at 01:50
  • Nevermind, found answer here: http://stackoverflow.com/questions/2695671/nswindowcontroller-windowdidload-not-called It works now! Thanks everyone! – Peter Schury Oct 04 '13 at 01:56