showWindow: Only Sometimes Showing the Winow

Discussion in 'Mac Programming' started by Uthalin, Mar 14, 2008.

  1. macrumors newbie

    Joined:
    Feb 26, 2008
    #1
    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.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    In IB, make sure visible at launch is unchecked, and release when closed is unchecked as well.
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    I generally use makeKeyAndOrderFront: instead of showWindow:, to do this.
     
  4. thread starter macrumors newbie

    Joined:
    Feb 26, 2008
    #4
    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.
     
  5. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #5
    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.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     
  7. macrumors newbie

    Joined:
    Jun 14, 2008
    #7
    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.
     
  8. macrumors newbie

    Joined:
    Oct 26, 2007
    #8
    Awesome! Thanks to google and this post, I got this problem fixed! Thanks!

    jorj
     

Share This Page