Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rebello95

macrumors member
Original poster
Jun 15, 2011
40
0
USA
Hey. I'm trying to figure out how to make it so that when someone clicks a button, it closes the window that was currently open, then opens a new window in the same application. How can I do this?

Thanks!
 
Hey. I'm trying to figure out how to make it so that when someone clicks a button, it closes the window that was currently open, then opens a new window in the same application. How can I do this?

Thanks!

Start reading the NSWindowController documentation.
 
Notifications

Since you mentioned two .xib files in your project I assume that you need to communicate between two .xibs. You can't establish a connection between two of these files using IBOutlets, etc. The way to do this is to use notifications. For example in the object methods of the first .xibs file owner, when the window is closed post a notification:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"firstWindowHasClosed" object: nil]; //post notification so other window knows to open

And in the other file owner object that controls the other window:

- (id) init //override class init so you can register it as an observer
{
if(self = [super init]) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver: self selector: @selector(openNewWindow: ) name: @"firstWindowHasClosed" object: nil];

Then when your first window posts the notification the second object receives the notification and calls the openNewWindow method. Be sure to call:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver: self];

in the object's dealloc method.
 
Last edited:
Also it's important to mention that notifications happen right away. When you post a notification the listeners are notified immediately. They don't wait for the event cycle.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.