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

亡梦爱人 提交于 2019-12-02 07:36:00

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!