Nuno Gonçalves
Nuno Gonçalves

Reputation: 6805

Swift - Is it possible to build the menu bar item outside AppDelegate class in a MacOS ManuBar App?

I'm doing a lot at once and this is quite overwhelming sometimes. Learning how to build a mac os (with menu bar and a settings window) while learning swift and sometimes trying to adapt objective c tutorials into swift.

I have followed this tutorial to help me get started with an app with a menubar. I have made it work, but now I want to improve it a little bit.

All the menu setup in this example is within the AppDelegate class. Since my app has a little more stuff in it, a settings window where I also have to play with tables, I wanted to clean the menu related stuff to a responsible class I called MenuLoader.

I moved all menu building related code from AppDelegate to MenuLoader, and I instantiate MenuLoader on AppDelegate's applicationDidFinishLaunching method.

When I run the code though, the app menu bar item appears for what seems the time it is being loaded, with some breaking points I can see clearly that the objects exist and have expected properties, but then the it just disappears. I'm afraid I'm missing some connection the AppDelegate uses to keep a relation of the menu bar referenced.

So my question is: is it possible to have a separate class handling only the status menu bar instead of having a big mess on the AppDelegate? If you think this might be a matter of my implementation, I'll post some code here, though I'm not sure it will make much of a difference.

Upvotes: 2

Views: 1513

Answers (1)

DPlusV
DPlusV

Reputation: 14336

Of course it's possible to have a separate class. Moving code into another class doesn't change the functionality as long as your logic is the same.

If something just disappears it's possible that you're not holding a strong reference to that object, and so it gets released as soon as it's out of scope. You might need:

 class MyClass {
     var:MyObject! // use this to store the reference to your object

     ...
 }

If this doesn't help, please post some of your code.

Upvotes: 2

Related Questions