何时从NSMenu删除动态NSMenuItem [英] When to remove dynamic NSMenuItem's from an NSMenu

查看:85
本文介绍了何时从NSMenu删除动态NSMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSMenu,其中包含许多动态项.这些项目是在NSMenuDelegate的menuNeedsUpdate方法中创建的,并分别分配了一个presentedObject.我希望修剪对象图,并希望通过关闭所有动态项(并在重新打开菜单时重新创建它们)来关闭菜单时重新获得一点内存.我遇到的问题是确切地知道应在何处以及如何处理.

I have an NSMenu which contains a number of dynamic items. These items are created in NSMenuDelegate's menuNeedsUpdate method and are each assigned a representedObject. I'm looking to trim the object graph and hopefully regain a bit of memory when the menu is closed by removing all dynamic items (and having them recreated when the menu is re-opened). The issue I'm having is knowing exactly where and how this should be handled.

文档指出,NSMenu的menuDidClose不是更新菜单内容的合适位置.我已经设置了一个监听NSMenuDidEndTrackingNotification的通知,并且正在通过调用NSMenu的removeItem:方法来删除动态项.这是处理这种情况的正确方法吗?

The documentation states that NSMenu's menuDidClose is not a suitable place to update the contents of a menu. I've setup a notification to listen for NSMenuDidEndTrackingNotification and am removing the dynamic items at that point with a call to my NSMenu's removeItem: method. Is this the correct way to handle this situation?

最后,我可以确保在NSMenuItem上调用removeItem可以正确使可能分配给它的任何自定义视图以及任何子菜单无效吗?

Finally, can I be assured that calling removeItem on an NSMenuItem will correctly nullify any custom views that may be assigned to it, as well as any submenus?

推荐答案

我最近遇到了这个问题.你是绝对正确的. menuDidClose:不是删除菜单项的地方.我试过了,但是它导致动作方法没有从菜单项中发送出去.据我所知,我发现正确的解决方案是使用 NSMenuDidEndTrackingNotification 通知.它也很好用.

I had this problem recently. You're absolutely right. menuDidClose: is not the place to delete menu items. I tried it, but it caused action methods to not be sent from the menu items. I found that the correct solution is, as far as I can see, to use the NSMenuDidEndTrackingNotification notification. It also works great.

是的, removeItem 应该释放菜单项.如果您有一些自定义的 NSMenuItem 子类在其中显式分配了一些视图,那么您当然必须根据需要实现 dealloc 方法.如果您使用的是垃圾回收,则完全不必为此担心.无论如何,您都应该始终使用Instruments来确保没有任何泄漏.

Yes, removeItem should release the menu item. If you have some custom NSMenuItem subclass where you explicitly allocate some views, you'll of course have to implement the dealloc method as necessary. If you're using garbage collection, shouldn't have to worry about this at all. In any case, you should always use Instruments to make sure you're not having any leaks.

这篇关于何时从NSMenu删除动态NSMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆