0

I have a NSPanel, which I want to set as keyWindow so that it should post NSWindowDidBecomeKeyNotification but, even on calling the makeKeyWindow method, the notification is not being posted. Below is my code, but I am not able to figure out what is wrong with it:

newPanel = [[NSPanel alloc]initWithContentRect:windowRect
                                         styleMask:NSUtilityWindowMask
                                           backing:NSBackingStoreBuffered 
                                             defer:FALSE];

    [[NSNotificationCenter defaultCenter]addObserver:self 
                                            selector:@selector(test:) 
                                                name:@"NSWindowDidBecomeKeyNotification" 
                                              object:newPanel];
    // Create a slider and add to the panel
    NSSlider *slider = [[NSSlider alloc]initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)];
    [[newPanel contentView]addSubview:slider];
    // Set window attributes
    [newPanel setHasShadow:TRUE];
    [newPanel setFloatingPanel:TRUE];
    [newPanel setReleasedWhenClosed:TRUE];
    [newPanel setHidesOnDeactivate:TRUE];
    [newPanel setBecomesKeyOnlyIfNeeded:NO];
    [newPanel makeKeyWindow];
    [newPanel makeFirstResponder:slider];
    [newPanel orderFront:sender];

Now when I call [newPanel makeKeyWindow]; the NSWindowDidBecomeKeyNotification should be posted and -(void)test:(NSNotification*)aNotification should get called. But it isn't getting called.

rsharma
  • 612
  • 6
  • 20

1 Answers1

1

I copied and pasted your code into a new project, and it didn't even display the panel, does it for you? I could make it work by making a property, thePanel (the compiler didn't like the name newPanel, not sure why) and changing all references of thePanel to self.thePanel. Also I added NSTitledWindowMask to the style mask argument (styleMask:NSUtilityWindowMask | NSTitledWindowMask) and either moved the makeKeyWindow command to after the orderFront command or replaced those two with makeKeyAndOrderFront:self.window.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Yes its displaying the panel just fine. Also the notifications are being posted after adding `NSTitledWindowMask`. But the panel is now displaying a title bar which is at all required in my case. Anyway we can remove the title bar and still get the notifications posted. – rsharma Jul 23 '12 at 04:42
  • I guess I got my answer here http://stackoverflow.com/questions/4946342/why-nswindow-without-stylemasknstitledwindowmask-can-not-be-keywindow – rsharma Jul 23 '12 at 07:30