Question

I have a Safari browser plugin, and in it, I want to open an NSWindow to display a copyright note. While the Ok button in the dialog closes the dialog's window, and works perfectly, when I click on the red close window "x" in the top left corner, it also closes the window, but it's parent window (the entire browser tab, I had the plugin running in), remains disabled, as if still a modal window was open somewhere.

I even tried to attach a new selector to the window close notification, which runs the same code as the Ok button, but still, it's not working properly.

Here is the relevant part of the code:

- (void) closeBox
{
    // called when the Ok button pressed
    [NSApp abortModal];

}

- (void)closeClicked:(NSNotification *)notification
{
    // called when the close window 'x' button pressed
    NSLog(@"Closed");
    [NSApp abortModal];
}


- (void) openBox
{
    NSRect frame = NSMakeRect(0, 0, 300, 250);
     mwin  = [[[NSWindow alloc] initWithContentRect:frame
                                     styleMask:NSClosableWindowMask |NSTitledWindowMask
                                       backing:NSBackingStoreBuffered
                                         defer:NO] autorelease];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(closeClicked:)
                                             name:NSWindowWillCloseNotification
                                           object:mwin];
    NSButton * bn;
    bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];

    [bn setButtonType:NSMomentaryPushInButton];
    [bn setTitle:@"Ok"];
    [bn setTarget:self];
    [bn setAction:@selector(closeBox)];

    [[mwin contentView] addSubview:bn];

    [NSApp runModalForWindow:mwin];   
}
Was it helpful?

Solution 2

After several hours of googling, I found others had similar problems, and the solution is pretty simple: the window object was retained by the NSNotificationCenter's observer. All I had to do it is to remove the observer:

- (void) closeClicked:(NSNotification *)notification
{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [NSApp abortModal];

    NSLog(@"Closed");

}

Now it's working properly.

OTHER TIPS

I have modified your code try like this below:-

- (void) closeBox
{
    // called when the Ok button pressed
  //Commented this line 
 // [NSApp abortModal];

    [mwin performClose:mwin];// Modified this line 


}

   //Modified below notification just comment the parameter
    - (void)closeClicked/*:(NSNotification *)notification*/
    {

    [NSApp abortModal];

}


- (void) openBox
{
    NSRect frame = NSMakeRect(0, 0, 300, 250);
    mwin  = [[[[NSWindow alloc] initWithContentRect:frame
                                         styleMask:NSClosableWindowMask |NSTitledWindowMask
                                           backing:NSBackingStoreBuffered
                                            defer:NO]retain]autorelease];
  //Modified notification below    

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(closeClicked)
                                                 name:NSWindowWillCloseNotification
                                               object:nil];
    NSButton * bn;
    bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];

    [bn setButtonType:NSMomentaryPushInButton];
    [bn setTitle:@"Ok"];
    [bn setTarget:self];
    [bn setAction:@selector(closeBox)];

    [[mwin contentView] addSubview:bn];

    [NSApp runModalForWindow:mwin];   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top