UITabBarController 应用程序 - 如何在控制器之间调用方法?
我有一个我认为非常简单的问题,但我却找不到解决方案。我有一个 UITabBarController 应用程序。有两个视图,我将它们称为 A 和 B。当然,我有一个初始化选项卡栏的 AppDelegate 类。
视图 B 有一个名为clearScore: 的按钮。当按下时,视图 B 需要直接或间接调用视图 A 上的clearScore:。有人可以告诉我实现此操作的步骤吗?感谢您的帮助!
I have what I assume is a very simple problem, but the solution has escaped me. I have a UITabBarController app. There are two views, I'll call them A and B. And of course I have an AppDelegate class that initializes the tab bar.
View B has a button called clearScore:. When it is pressed, view B needs to invoke directly or indirectly clearScore: on view A. Can someone show me the steps to make this happen? Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 通知 或 < a href="http://www.mindsizzlers.com/tag/key-value-observing/" rel="nofollow">键值观察 (KVO)。
假设您有一个模型对象,其中包含您的属性分数。现在,您将 viewController B 中的 Key-Value-Observer 添加到模型实例的 Score 属性中。当您在 A 中按clearScore 时,您将分数属性设置为 0(或 nil)。观察者将通知 B 属性发生变化,以便您可以轻松更新 B 的视图。
You can use Notifications or Key-Value-Observing (KVO).
Let's assume you've got a model object in which your property score resides. Now you add a Key-Value-Observer in you viewController B to the score property of the model instance. When you press clearScore in A you set the score property to 0(or nil). The Observer will inform B that the property changed so you can easily update your view of B.
我认为有一个更简单的方法来实现这一点:
您可以在 bViewController 中使用类似以下代码的内容:
或者:
不要忘记在 bViewController 的标头中
#import "aViewController.h"
;I think there is a more simple way to achieved that:
You can use something like the below code in bViewController:
Or:
Don't forget to
#import "aViewController.h"
in bViewController's header;视图应该只直接与它们自己的控制器对话,并且控制器不应该与除了它自己的控制器之外的视图对话。如果视图控制器 B 的按钮之一应导致消息发送到视图控制器 A,则该按钮应触发控制器 B 中的操作,进而将消息发送到 A。
但是, -clearScore: 听起来像是一种方法模型的一部分而不是控制者的一部分,B 拥有利益的事实进一步证明了这一点。您可能需要考虑稍微重构一下您的代码。
Views should talk directly only to their own controllers, and a controller shouldn't talk to views other than its own. If one of view controller B's buttons should result in a message being sent to view controller A, then the button should trigger an action in controller B that in turn sends a message to A.
However, -clearScore: sounds like a method that would be part of a model rather than part of a controller, and the fact that B has an interest is further evidence of the same. You might want to think about refactoring your code a bit.