Raffael
Raffael

Reputation: 1139

Running NSAlert within NSPopover

we are developing a menu bar item app, and I'd love to write a NSAlert category which shows the alert within a NSPopover, that appears below the NSStatusItem.

So far, the category implements the following new method:

- (void) runAsMenuItemPopUpWithCompletionBlock:(NSAlertCompletionBlock)block {

    // Get content view of NSAlert
    NSView *alertContentView = [self.window contentView];

    // Ask the menu item to show the view as a NSPopOver
    [[GFMenuItem sharedInstance] popOverView:alertContentView];

    // (...) Handle response with callback
}

But opening an alert

NSAlert *alert = [NSAlert alertWithMessageText:@"Learn more?" defaultButton:@"Learn more" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Do you want to view detailed information?"];
[alert runAsMenuItemPopUpWithCompletionBlock:nil];

results in the following visualization:

NSAlert in NSPopover

The problem is the third empty button, the help button and the Checkbox, which have not been set up to be shown. Any idea on how to get rid of them if they have not been set up?

Upvotes: 2

Views: 696

Answers (1)

Raffael
Raffael

Reputation: 1139

Turns out you can call [alert layout] to trigger the manual layout processing. It will hide any buttons which aren't set up to show!

Corrected method:

- (void) runAsMenuItemPopUpWithCompletionBlock:(NSAlertCompletionBlock)block {

    // Trigger the layout processing and get content view of NSAlert
    [self layout];
    NSView *alertContentView = [self.window contentView];

    // Ask the menu item to show the view as a NSPopOver
    [[GFMenuItem sharedInstance] popOverView:alertContentView];

    // (...) Handle response with callback
}

Upvotes: 1

Related Questions