-1

I'm working with storyboard and this is the code I have below.

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
  NSStatusItem *status_item = [[NSStatusBar systemStatusBar] statusItemWithLength: -1];
    [status_item setMenu:_menu_bar_item];
    status_item.button.image = [NSImage imageNamed:@"MenuBarIcon"];
    status_item.button.imagePosition = NSImageLeft;
    status_item.button.title = @"foo";
    status_item.button.toolTip = @"bar";
    [status_item setVisible:true];
}

The status item does show in the menu bar for like a frame and then instantly dissapears. I've tried it with and without the image and I can't get it to show longer than a split second before it just vanishes. Does anyone know what causes this and how to prevent it?

SeizureSalad
  • 23
  • 1
  • 6
  • Or does this answer your question? [NSStatusBar + Swift: title shows and immediately disappear](https://stackoverflow.com/questions/44567756/nsstatusbar-swift-title-shows-and-immediately-disappear) – Willeke Jan 27 '22 at 08:08

1 Answers1

1

You are creating the object and then you throw it away.

You need a strong reference for example a property

@property (strong) NSStatusItem *status_item;

and then

status_item = [[NSStatusBar systemStatusBar] statusItemWithLength: -1];
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thank you! I knew you had to have a strong refrence for the menu but I didn't know you needed one for the status item as well. – SeizureSalad Jan 26 '22 at 21:55
  • @SeizureSalad `NSStatusItem ` has a strong reference to its menu, you don't to have one. – Willeke Jan 27 '22 at 08:10