2

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?

Raffael
  • 1,119
  • 10
  • 20
  • If the behaviour you're experiencing contradicts what the documentation says (and in this case, the documentation clearly says that providing `nil` for the `otherButton:` argument should result in that button not being created) then you should file a bug with Apple. – dreamlax May 01 '13 at 13:38
  • Doesn't seem to be a real bug, see my answer. Thanks anyway! – Raffael May 01 '13 at 14:28

1 Answers1

1

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
}
Jay
  • 6,572
  • 3
  • 37
  • 65
Raffael
  • 1,119
  • 10
  • 20