Preference panel appears one but not the second time?

  • I'm trying to make an app with an empty window and an empty Preference panel that is activated when user choses Preferences... from the menu. I've tried to accomplish that with the following code:

    //
    //  AppController.h
    #import <Cocoa/Cocoa.h>
    @class PreferenceController;

    @interface AppController : NSObject {
        PreferenceController *preferenceController;
    }
    - (IBAction)showPreferencePanel:(id)sender;
    @end

    //
    //  AppController.m
    #import "AppController.h"
    #import "PreferenceController.h"

    @implementation AppController

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

    - (void)dealloc
    {
        [preferenceController release];
        [super dealloc];
    }
    @end

    //--------------------

    //
    //  PreferenceController.h
    #import <Cocoa/Cocoa.h>

    @interface PreferenceController : NSWindowController {

    }
    @end

    //
    //  PreferenceController.m
    #import "PreferenceController.h"

    @implementation PreferenceController
    - (id)init
    {
        self = [super initWithWindowNibName:@"Preferences"];
        return self;
    }
    - (void)windowDidLoad
    {
        NSLog(@"Nib file is loaded");
    }
    @end

    I'm using Leopard. In the IB, I've done done following

    1. From Controllers I drag and dropped a blue box NSObject and chose AppController as its class.

    2. From IB menu chose File/New.. and chose Cocoa/Empty. Saved as Preference.nib in the same directory as MainMenu.nib. (I thought that maybe IB would add this file automatically to my project, but it didn't do it so I dragged it manually from Finder to Resources in Xcode).

    3. From XCode dragged PrefrenceController.h to Preferences.nib in IB. I then set File's owner to PreferenceController.

    4. Dragged a Panel from Objects/Application/Windows  and dropped it on the screen. A Panel icon appeared in the Preferences.nib.

    5. I then built and run. An empty window appeared. I chose Preferences... from the menu and the Preference panel appeared. I closed the Preference panel and tried to open it again by choosing Preferences... from the menu. But the panel didn't appear. What is the explanation??

    Here are some clues

    If I change
      if (!preferenceController) {
          preferenceController = [[PreferenceController alloc] init];
      }

    to
        //if (!preferenceController) {
          preferenceController = [[PreferenceController alloc] init];
      //}

    then the app works. However the preference panel does not become activated unless I click on it.

    Thanks Bob

          ____________________________________________________________________________________
    Never miss a thing.  Make Yahoo your home page.
    http://www.yahoo.com/r/hs
  • On 5 Dec 2007, at 9:05 AM, Bob Ueland wrote:

    > 4. Dragged a Panel from Objects/Application/Windows  and dropped it
    > on the screen. A Panel icon appeared in the Preferences.nib.
    >
    > 5. I then built and run. An empty window appeared. I chose
    > Preferences... from the menu and the Preference panel appeared. I
    > closed the Preference panel and tried to open it again by choosing
    > Preferences... from the menu. But the panel didn't appear. What is
    > the explanation??

    _Two_ question marks just add panic. They won't make it more of a
    question. A peeve of mine.

    I won't give an explanation, but perhaps a possible fix will do:

    In Interface Builder, select the panel, then command-1 to see the
    attributes. Uncheck "Release When Closed."

    (This was verified in IB 3, but I think it's still applicable in IB 2.
    I haven't verified the actual behavior, though.)

    — F
  • Thanks Fritz,

    I was really glad when I received your answer and thought that it must be the explanation, but  unchecking "Release When Closed." didn't make any difference. So the explanation must be something else.

    ----- Original Message ----
    From: Fritz Anderson <fritza...>
    To: Bob Ueland <bobueland...>
    Cc: <cocoa-dev...>
    Sent: Wednesday, December 5, 2007 4:23:54 PM
    Subject: Re: Preference panel appears one but not the second time?

    On 5 Dec 2007, at 9:05 AM, Bob Ueland wrote:

    > 4. Dragged a Panel from Objects/Application/Windows  and dropped it
    > on the screen. A Panel icon appeared in the Preferences.nib.
    >
    > 5. I then built and run. An empty window appeared. I chose
    > Preferences... from the menu and the Preference panel appeared. I
    > closed the Preference panel and tried to open it again by choosing
    > Preferences... from the menu. But the panel didn't appear. What is
    > the explanation??

    _Two_ question marks just add panic. They won't make it more of a
    question. A peeve of mine.

    I won't give an explanation, but perhaps a possible fix will do:

    In Interface Builder, select the panel, then command-1 to see the
    attributes. Uncheck "Release When Closed."

    (This was verified in IB 3, but I think it's still applicable in IB 2.

    I haven't verified the actual behavior, though.)

        — F

          ____________________________________________________________________________________
    Looking for last minute shopping deals?
    Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today