PDA

View Full Version : showWindow: Only Sometimes Showing the Winow




Uthalin
Mar 14, 2008, 11:53 AM
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:


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


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


- (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
Mar 14, 2008, 04:38 PM
In IB, make sure visible at launch is unchecked, and release when closed is unchecked as well.

Eraserhead
Mar 14, 2008, 09:35 PM
I generally use makeKeyAndOrderFront: instead of showWindow:, to do this.

Uthalin
Mar 15, 2008, 12:12 PM
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
Mar 15, 2008, 02:14 PM
Hmm. In my sub of NSWindowController I have this as the init method:

- (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
Mar 15, 2008, 02:23 PM
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
Jun 14, 2008, 08:14 PM
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
Aug 3, 2008, 09:04 PM
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