rsharma
rsharma

Reputation: 642

NSPanel not becoming key window

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.

Upvotes: 0

Views: 970

Answers (1)

rdelmar
rdelmar

Reputation: 104082

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.

Upvotes: 1

Related Questions