Question

I am looking for the simplest example which shows one window opening another window and then closing it.

The place where I seem to get stuck is with the NIBs and how the outlets should be wired. I can easily get the window created; closing it presents a challenge.

Any advice much appreciated!

I'm attaching my code below. I think what I am trying to do is simple, yet it doesn't work. Thanks to Francis I can now get the window to open and close but once I try to open it again the application crashes (EXC_BAD_ACCESS). I'm sure this is due to my poor understanding of the NIB relationship to classes. In most languages I would just need to instantiate a new instance of a window and then close it.

I have 2 windows in MainMenu.xib. (I'd prefer to have the window in a separate NIB but that appears to create other problems!) The AppDelegate has 2 outlets, to window (original) and otherWindow (2nd window created).

The first window has 2 buttons: "Open Window" and "Close Window" with connections to the 2 methods in the code.

Code:

MyTestAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface MyTestAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    NSWindow *otherWindow;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSWindow *otherWindow;

- (IBAction)openOtherWindow:(id)sender;
- (IBAction)closeOtherWindow:(id)sender;

@end

MyTestAppDelegate.c

#import "MyTestAppDelegate.h"

@implementation MyTestAppDelegate

@synthesize window;
@synthesize otherWindow;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)openOtherWindow:(id)sender
{
    [otherWindow makeKeyAndOrderFront:sender];
}

- (IBAction)closeOtherWindow:(id)sender
{
    [otherWindow close];
}
@end
Was it helpful?

Solution

Basically, in your NIB you create the various windows, add your buttons, textfields, etc. Then you add a custom object that acts as a "controller" that sends and receives messages to and from the windows and various controls. In a simple project you can use the automatically-created AppDelegate object to control your windows, for bigger projects you want a separate object to handle the logic, which can be an instance of NSObject or NSWindowController, depending on your needs.

Windows can be set up as "Visible at launch" which opens them when your app launches. You can also open them manually by creating IBOutlet references in the header files and connecting them in the NIB. To show windows you send them a makeKeyAndOrderFront: message. To close them you send them a close method.

To respond to window opening/closing you assign your controller object as the windows' "delegate", which means it will recieve messages from the windows, which are listed in the documentation under the NSWindowDelegate protocol. So if you wanted to open a window in response to another window closing, you would listen for the windowWillClose: message and tell the other window to open, and vice-versa.

This is pretty basic stuff so I recommend you read the Hillegass book, or browse some of the various tutorials available online.

EDIT:

You're application is crashing because you have your otherWindow set to "Release when closed" Since you are in a memory-managing environment, and nothing is retaining the window, the next time you try to open it it has already been freed. The solution is to uncheck "Release When Closed" in the NIB file. Notice you can also connect your button actions to makeKeyAndOrderFront: and performClose: methods of the other window itself, directly in the NIB.

OTHER TIPS

Create a new project by using the "UINavigationController" template in XCode 4.1 or the "Master-Detail Application" template in XCode 4.2 and have a look at how the stuff is working there.

If you are not interested in learning by reviewing the XCode template have a look at following tutorial: http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/

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