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

Uthalin

macrumors newbie
Original poster
Feb 26, 2008
3
0
I've just started working with Cocoa and Objective-C and have been following along the second edition of Cocoa Programming for Mac OS X. I'm working in Xcode 3 instead of 2.5 so I've had to work around that a bit, but for the most part I've been fine. However, for the life of me I can't get a Preference panel to work correctly.

PreferenceController is a class that extends NSWindowController. The interface is being loaded from Preference.nib. This is the init method I have in PreferenceController:

Code:
- (id) init
{
	self = [super initWithWindowNibName:@"Preferences"];
	return self;
}

And here is what I am doing to actually load the window. (This is in AppController):

Code:
- (IBAction) showPreferencePanel: (id) sender
{
	NSLog(@"Showing Preferences");
	
	//Is preferenceController nil?
	if(!preferenceController)
		preferenceController = [[PreferenceController alloc] init];
	
	[preferenceController showWindow: self];
}

I know showPreferencePanel is being called because I get the NSLog message. The first time the preference button is pressed since the application is loaded it works perfectly. However, once it is closed and you try again nothing happens. I added a check to export to the log to see if the window is loaded, and it says it is, even when it does not show. Shouldn't showWindow just show the window if it has been loaded? Is there some method I need to override that I'm not?

If I comment out the [preferenceController showWindow: self] line I get the exact same behavior, which suggests that it is doing nothing. If you uncheck "Visible at launch" in IB then it is never displayed.

I'm sure I'm missing something really dumb, but I just can't figure out what that is.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
In IB, make sure visible at launch is unchecked, and release when closed is unchecked as well.
 

Uthalin

macrumors newbie
Original poster
Feb 26, 2008
3
0
I generally use makeKeyAndOrderFront: instead of showWindow:, to do this.

I ended up just having it reload the nib each time, but this is still bothering me. It's a NSWindowController so this should work, right?

[[preferenceController window] makeKeyAndOrderFront: self];

It does the exact same thing as [preferenceController showWindow: self]: nothing.
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
Austin, TX
Hmm. In my sub of NSWindowController I have this as the init method:

Code:
- (id)init {
	self = [self initWithWindowNibName: @"PopupWindow"];
	if (self) {
	//
	}
	return self;
}

Note that I am using [self initWithWindowNibName:] and you are calling [super ...].

Maybe that's what the difference is.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I think technically you're supposed to use super, since you're a subclass of NSWindowController. If you were an NSObject subclass it wouldn't really matter but it's best to use super.

Uthalin: try using the debugger to see if your objects are getting released prematurely.
 

MacFanBoy42

macrumors newbie
Jun 14, 2008
1
0
I had this same problem. It ended up that I had not connected the PreferenceController's window outlet. Once that was connected it works great.
 

sirjorj

macrumors newbie
Oct 26, 2007
28
4
I had this same problem. It ended up that I had not connected the PreferenceController's window outlet. Once that was connected it works great.

Awesome! Thanks to google and this post, I got this problem fixed! Thanks!

jorj
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.