如何在 Cocoa 中将控件绑定到单例?
我的 FTP 应用程序中有一个单例,旨在存储该应用程序可以处理的所有类型的服务器,例如 FTP 或 Amazon S3。这些类型是位于应用程序包中的插件。它们的路径由 applicationWillFinishLoading: 定位,并发送到单例内的 addServerType: 方法,以加载并存储在 NSMutableDictionary 中。
我的问题是这样的:
如何将 NSDictionaryController 绑定到单例实例内的字典?可以在IB中完成,还是必须在代码中完成?我需要能够在 NSPopupButton 中显示字典的键,以便用户可以选择服务器类型。
预先感谢!
球体猫1
I have a singleton in my FTP app designed to store all of the types of servers that the app can handle, such as FTP or Amazon S3. These types are plugins which are located in the app bundle. Their path is located by applicationWillFinishLoading: and sent to the addServerType: method inside the singleton to be loaded and stored in an NSMutableDictionary.
My question is this:
How do I bind an NSDictionaryController to the dictionary inside the singleton instance? Can it be done in IB, or do I have to do it in code? I need to be able to display the dictionary's keys in an NSPopupButton so the user can select a server type.
Thanks in advance!
SphereCat1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到/弥补了这个问题的答案:我只是重写 init 方法,这样当从 XIB 文件调用它时,它仍然返回单例。然后,我提供一个名为 realInit 的方法,在第一次调用 init 时进行实际初始化。
代码:
编辑:当然,您需要在类实现的顶部将 _sharedInstance 定义为静态变量:
另外编辑:因为您现在确定您的 init 方法将至少被调用一次,所以您可以继续将普通的单例共享实例方法替换为:
如果有人发现此设计有任何明显问题,请告诉我!
球体猫1
I found / made up the answer to this: I simply override the init method so when it's called from the XIB file, it still returns the singleton. I then provide a method named realInit to do an actual initialization the first time init is called.
Code:
EDIT: Of course you'll need to define _sharedInstance as a static variable at the top of your class implementation:
ALSO EDIT: Since you now know for sure that your init method will be called at least once, you can go ahead and replace your normal singleton sharedInstance method with this:
If anyone sees any obvious problems with this design, please let me know!
SphereCat1