angry_kiwi angry_kiwi - 1 year ago 94
Objective-C Question

status bar icon showed for one second then disappear

I'm trying some stuff, to test the status bar icon feature.

I think I got the all the functions right. However when run. The icon displayed for about 1 mili second then disappeared.

Below is the screenshot of my code:

the code

source code:

Demo video:

Answer Source

Retain statusItem object.

statusItem=[[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]retain];  

From Documentation of statusItemWithLength:.

The receiver does not retain a reference to the status item, so you need to retain it. Otherwise, the object is removed from the status bar when it is deallocated.