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

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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

Moderator emeritus
Jun 15, 2000
7,958
7
Yes, global or static variable. I usually do something like this in the nib's controller:

Code:
+ (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:

Code:
[[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

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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
 

satyam90

macrumors regular
Jul 30, 2007
242
0
Bangalore, India
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?
 

satyam90

macrumors regular
Jul 30, 2007
242
0
Bangalore, India
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
 

Attachments

  • testing.zip
    38.2 KB · Views: 75

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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.
 

Attachments

  • testingMR2.zip
    53.3 KB · Views: 88

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors regular
Jul 30, 2007
242
0
Bangalore, India
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

macrumors regular
Jul 30, 2007
242
0
Bangalore, India
Code:
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.