Creating status item - icon shows up, menu doesn't

前端 未结 1 800
暖寄归人
暖寄归人 2021-01-28 22:53

In a document-based project I am trying to create a status menu. I have a singleton class that builds the status bar, and I am initiating it from an application delegate, as you

相关标签:
1条回答
  • 2021-01-28 23:37

    You declared myStatusMenu as an outlet, but never loaded a nib (or assigned anything to it yourself). An outlet cannot get objects out of nowhere; the outlet is set only when you load a nib that has the outlet connected to something (or assign something to the variable yourself, as if it weren't an outlet).

    You can prove this by adding a line to buildStatusItem that logs the value of the myStatusMenu instance variable. I expect that it will be nil.

    What you need to do is:

    1. Create a nib to contain the status item's menu.
    2. Set the class of the File's Owner to KBStatusMenu.
    3. In KBStatusMenu, implement init to load the nib you just created.

    Then, by the time you reach buildStatusItem, loading the nib will have set the outlet, and you will have a menu to give to your status item.

    I would recommend only creating one KBStatusMenu instance. In this case, I recommend enforcing the singleton: init should test whether gInstance has already been set and, if so, return that; only if it hasn't should it initialize and return self.

    0 讨论(0)
提交回复
热议问题