PDA

View Full Version : Cocoa In XCode: How can I use two .xib files in 1 project?




rebello95
Nov 26, 2011, 05:35 PM
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!



gnasher729
Nov 27, 2011, 06:04 AM
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.

Senor Cuete
Dec 6, 2011, 04:04 PM
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.

rebello95
Dec 6, 2011, 04:11 PM
Thanks!

Senor Cuete
Dec 6, 2011, 09:04 PM
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.