Swift -是否可以在MacOS ManuBar应用程序中构建AppDelegate类之外的菜单栏项目?

时间:2022-06-01 22:14:16

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.

我做了很多事情,有时这是非常难以承受的。学习如何构建一个mac操作系统(带有菜单栏和设置窗口),同时学习swift,有时尝试将objective c教程转换为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.

本例中的所有菜单设置都在AppDelegate类中。因为我的应用程序里有更多的东西,一个设置窗口,我还需要玩表格,我想把菜单上的相关内容清理给一个负责的类,我叫它MenuLoader。

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

我将所有与菜单构建相关的代码从AppDelegate移动到MenuLoader,并在AppDelegate的applicationDidFinishLaunching方法上实例化MenuLoader。

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.

当我运行代码时,应用程序菜单栏项会在加载时出现,我可以清楚地看到对象存在并具有预期的属性,但是它就会消失。恐怕我漏掉了AppDelegate用来保持菜单栏引用关系的一些连接。

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.

所以我的问题是:是否可能有一个单独的类只处理状态菜单栏,而不是在AppDelegate上弄得一团糟?如果您认为这可能与我的实现有关,那么我将在这里发布一些代码,尽管我不确定这是否会带来很大的不同。

1 个解决方案

#1


2  

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.

如果这没有帮助,请张贴一些您的代码。

#1


2  

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.

如果这没有帮助,请张贴一些您的代码。