如何处理多个控制器类之间的可可通信
我是可可的新手,也许这些问题非常基本。 目前我正在开发一个使用 NSTabView 的 Mac 应用程序,您可以在“应用程序”、“选项”和“统计信息”之间切换。
现在我的方法是创建三个控制器类:AppControler、OptionsController 和StatisticsController。我在 .xib 文件中为它们创建了对象,并将相应的 UI 元素链接到正确的控制器对象。
我的问题是: - 对于 NSTabView 有多个控制器对象,这是正确的方法吗? - 当我希望AppController获取有关选项的信息时,如何在App-和OptionsController之间进行通信?
谢谢!
i'm new to Cocoa, maybe these questions are very basic.
Currently I'm developing a mac application that uses an NSTabView, where you can switch between "app", "options" and "statistics".
Now my approach is to create three controller classes, an AppControler, an OptionsController and a StatisticsController. I created objects for them in the .xib file and linked the corresponding UI elements to the correct controller objects.
My questions are:
- Is that the right way, having multiple controller objects for an NSTabView?
- When I want the AppController to get information about the options, how can I do communication between App- and OptionsController?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我开始 Cocoa 开发时,我经常被这些类型的问题困扰。我最终发现没有真正的答案。在 Cocoa 和 Objective-C 中,剥皮的方法有很多种,最终取决于你。
由于所有控制器都将加载 Nib,因此最简单的方法是在每个控制器中为希望与之通信的其他控制器对象创建一个插座。在界面生成器中连接插座,然后您就可以开始了。
在我看来,这是一个简单而有效的解决方案。您最终会在控制器之间出现循环引用,但 Objective-C 的 Nib 加载/卸载代码将为您处理所有这些问题。
I struggled with these types of questions a lot when I started Cocoa development. I eventually found that there's no real answer. In Cocoa and Objective-C there are so many ways to skin a cat that in the end it really ends up being up to you.
Since all of your controllers are going to get loaded with the Nib, the easiest way to do it is to create an outlet in each controller for the other controller objects with which it wishes to communicate. Hookup the outlets in interface builder and you are set to go.
This is an easy and effective solution in my mind. You are going to end up with circular references between the controllers but Objective-C's Nib loading/unloading code is going to take care of all of that for you.
对于选项卡视图,我通常只为该窗口中的所有选项卡创建一个控制器,但如果使用超过 3 或 4 个选项卡,我可能会将其拆分,并且每个选项卡都需要大量代码来处理操作和销售点。
For tab views, I usually just make one controller for all of the tabs in that window, though I'd probably split it up if was using more than 3 or 4 tabs, and each tab required a significant amount of code to handle the actions and outlets.
通常,使用 MVC 范例。在您的情况下,如果一个控制器正在创建或修改某些设置,您可以以某种方式在模型中设置它(可以像某些用户默认设置一样简单),如果在其他地方使用它,您可以从模型中读取它。
Typically, an MVC paradigm is used. In your case, if one controller is creating or modifying some setting you set it in the model in some way (could be as simple as some user defaults set), and if thats used elsewhere you read it from the model.