PDA

View Full Version : Cocoa: Calling Nibs




MacDonaldsd
Sep 17, 2007, 11:43 AM
I have 2 separate Nib files that I want both to be able to call a third nib file.

The issue Im having is that it creates a separate instance of the third nib file for each and id like just the 1 Window which both can call.

What is the best way to go about fixing this ? Global Variable ?



kainjow
Sep 17, 2007, 01:01 PM
Yes, global or static variable. I usually do something like this in the nib's controller:

+ (id)sharedController
{
static id instance = nil;
if (!instance)
instance = [[[self class] alloc] init];
return instance;
}

- (id)init
{
if (self = [super initWithWindowNibName:@"MyWindow" owner:self])
{
}
return self;
}

So let's say the class is MyWindowController (subclass of NSWindowController). You can call it like so:

[[MyWindowController sharedController] showWindow:nil];

The first time you call sharedController, it loads the nib. Then all subsequent times it returns the same controller object.

MacDonaldsd
Sep 18, 2007, 04:06 PM
Thanks thats great. Sorry for my ignorance, but what does the + do in front of the method ?

Does that make it global ? or is that done by the use of static

kainjow
Sep 18, 2007, 05:31 PM
The + indicates that the method is a class method.

From The Objective-C Programming Language (http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf):

The names of methods that can be used by class objects, class methods, are preceded by a plus sign:

+ alloc;

The methods that instances of a class can use, instance methods, are marked with a minus sign:

- (void)display;

satyam90
Jan 2, 2008, 08:32 AM
Even after following the same way, the window is not showing up.
In IB, I connected delegate between window and controller also.
What will be the reason for not showing the window?

kainjow
Jan 2, 2008, 09:33 AM
Did you connect the controller's window outlet to the window?

satyam90
Jan 2, 2008, 11:30 PM
yes, i connected controllers outlet "window" and also windows delegate outlet to controller.

implemented the code u mentioned.

but it is not showing the window.

I am attaching the sample project also. please go through that and suggest me the wrong I did in it. I am using XCode 2.4.1

MacDonaldsd
Jan 3, 2008, 05:41 AM
I changed your code around a bit but your main problem was in interface builder, you have to set the File Owner's class to your "MyWindowCtrl" class.

satyam90
Jan 3, 2008, 06:10 AM
Can you please tell me what are the changes you made and also I want to know how to implement this in XCode....please....

MacDonaldsd
Jan 3, 2008, 09:53 AM
Your better off asking kainjow he is good at explain these things.

The return type for the shared controller is id

You had:

+ (MyWindowCtrl *)sharedInstanceValue

I changed to:

+ (id )sharedInstanceValue

Also as its a static variable you can declare it in the method. It lives for the duration of the programme.

Im using XCode 3 so showing you the steps in Interface builder is a bit awkward as you have Xcode 2.

satyam90
Jan 3, 2008, 10:44 PM
Can you please explain me Mr. Kainjowhow to do it in XCode 2.4.1

kainjow
Jan 3, 2008, 11:36 PM
When you're using NSWindowControllers (actually any custom nib), you need to have the nib's File Owner be set as your window controller class. So to do this, in IB select the File Owner object in the nib. Then select Custom Class in the Inspector. Under the Custom Class list, scroll to MyWindowCtrl and select it. Now connect your "window" outlet like you did before, but connect it with the File's Owner object.

satyam90
Jan 4, 2008, 02:57 AM
Thanks a lot for immediate response.....

satyam90
Jan 21, 2008, 11:29 PM
Hi kainjow, i am facing few problems now.
In my code, I have sharedController with init, a method "setData" which sets some variables in my class, a "windowDidBecomeMain" which sets default values of the controls in my UI.
My UI has a combo box whose values are updated in "windowDidDecomeMain" and a OK button with appropriate IBAction handler.
For showing up window, I am first creating the instance of class by called sharedController, then setData and then calling showWindow.

Now the problem I am facing is, the data that I am setting using setData is not available in windowDidBecomeMain or IBAction handler for OK.

When I traced, sharedController is called once, then init, then setData, again init, then windowDidBecomeMain.

The error I am suspecting is that calling "Init" second time is resetting my class variables.

What will be solution for this? Why Init is getting called again?

satyam90
Jan 30, 2008, 03:16 AM
static id instance = nil;
+ (id)sharedController
{
if (!instance)
instance = [[[self class] alloc] init];
return instance;
}

- (id)init
{
if (self = [super initWithWindowNibName:@"MyWindow" owner:self])
{
}
return self;
}



In the above code, I added dealloc method and releasing "instance" and making it nil. Also I am using [[self window]close] so that the window is closed and released. But next time, when I am creating sharedInstance and showing window, it is showing previous values in the textboxes I have in the window. Am I following right procedure?

Eraserhead
Jan 30, 2008, 06:53 AM
^^ Please only use one thread, not two (http://forums.macrumors.com/showthread.php?t=423769).