UITabBarController 应用程序 - 如何在控制器之间调用方法?

发布于 2024-10-30 06:27:15 字数 208 浏览 2 评论 0原文

我有一个我认为非常简单的问题,但我却找不到解决方案。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沧桑㈠ 2024-11-06 06:27:15

您可以使用 通知 或 < 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.

一片旧的回忆 2024-11-06 06:27:15

我认为有一个更简单的方法来实现这一点:

您可以在 bViewController 中使用类似以下代码的内容:

for (UIViewController* testViewController in self.tabBarController.viewControllers) {
    if ([testViewController respondsToSelector:@selector(clearScore)]) {
        [(aViewController *)testViewController clearScore];
    }
}

或者:

for (UIViewController* testViewController in self.tabBarController.viewControllers) {
    if ([testViewController isKindOfClass:[aViewController class]]) {
        [(aViewController *)testViewController clearScore];
    }
}

不要忘记在 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:

for (UIViewController* testViewController in self.tabBarController.viewControllers) {
    if ([testViewController respondsToSelector:@selector(clearScore)]) {
        [(aViewController *)testViewController clearScore];
    }
}

Or:

for (UIViewController* testViewController in self.tabBarController.viewControllers) {
    if ([testViewController isKindOfClass:[aViewController class]]) {
        [(aViewController *)testViewController clearScore];
    }
}

Don't forget to #import "aViewController.h" in bViewController's header;

神也荒唐 2024-11-06 06:27:15

视图应该只直接与它们自己的控制器对话,并且控制器不应该与除了它自己的控制器之外的视图对话。如果视图控制器 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.

心安伴我暖 2024-11-06 06:27:15
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    //other codes

    [self.tabBarController setDelegate:self]

    //other codes
    }

// UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController respondsToSelector:@selector(reloadDataTemp)]) {
        [(YourViewController *)viewController reloadData];
    }
}
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    //other codes

    [self.tabBarController setDelegate:self]

    //other codes
    }

// UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController respondsToSelector:@selector(reloadDataTemp)]) {
        [(YourViewController *)viewController reloadData];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文