ashgromnies
ashgromnies

Reputation: 3335

NSAlert won't disappear

I'm using a simple NSAlert based on Apple's sample code, and while it displays fine, it never disappears.

Code:

void DisplayAlert()
{
    NSAlert *alert = [[NSAlert alloc] init];
    NSLog(@"TEST");
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:@"Yay!"];
    [alert setInformativeText:@"This is an informational alert."];
    [alert setAlertStyle:NSAlertStyleInformational];

    [alert runModal];

    NSLog(@"TEST2");
    [alert.window close];
    [alert release];
    NSLog(@"TEST3");
}

I have tried with and without the [alert.window close] line and neither way will the alert disappear.

I have also tried making the first line [[[NSAlert alloc] init] autorelease]; but that did not help, either.

All of the NSLog messages appear.

Upvotes: 1

Views: 978

Answers (2)

eckenrod
eckenrod

Reputation: 529

I had this same problem and struggled for a long time trying to find a way to make the dismissed alerts disappear.

My solution was to forsake NSAlert altogether in favor of CFUserNotificationAlert. This blocking alert API or the non-blocking CFUserNotificationNotice API can be used to display stand-alone alert dialogs which are identical to those produced by NSAlert, but they can be dismissed unlike NSAlert dialogs when run from a simple windowless binary.

Here's an example of CFUserNotificationDisplayAlert and a preview of some of its code below:

#import <CoreFoundation/CoreFoundation.h>

int main(void) {
    CFOptionFlags cfRes;

    //display alert with 5 second timeout, at NoteAlertLevel
    CFUserNotificationDisplayAlert(5, kCFUserNotificationNoteAlertLevel, 
            NULL, NULL, NULL, 
            CFSTR("Testing"), 
            CFSTR("Click on any button..."), 
            CFSTR("OK"), 
            CFSTR("Cancel"), 
            CFSTR("Test Button"), 
            &cfRes);
    return cfRes;
}

Upvotes: 1

James Bucanek
James Bucanek

Reputation: 3439

The method you need is -orderOut: not -close. Alert/Panel windows are not documents and are not "closed" in the usually sense. You just what them to disappear.

Upvotes: -1

Related Questions