iPhone - 在选项卡栏中切换视图控制器并调用方法

发布于 2024-08-17 11:36:10 字数 1031 浏览 2 评论 0原文

我的应用程序中有一个 TabBar,我在 AppDelegate 中执行此操作:

...
test2ViewController = [[Test1ViewController alloc] init];
...
navigationTest2Controller = [[UINavigationController alloc] initWithRootViewController:test2ViewController];

NSArray *myControllers = [NSArray arrayWithObjects:..., navigationTest2Controller, nil];
[self.myTabBarController setViewControllers:myControllers animated:NO];

现在我遇到的问题是我位于 ViewController 中,并且我想切换到“navigationTest2Controller”。我在我的 AppDelegate 中执行此操作:

self.myTabBarController.selectedViewController = navigationTest2Controller;

这有效。它切换到这个 ViewController!这个ViewController已经被加载并且viewDidLoad方法被调用。在此 viewDidLoad 方法中是一个方法调用:

[self myMethod];

我希望,如果视图切换到此 ViewController,则应始终调用此“myMethod”。 我该怎么做?在我的 AppDelegate 行之前

self.myTabBarController.selectedViewController = navigationTest2Controller;

???或者是否有另一个委托在每次选择/切换到 ViewController 时都会被调用?

有人知道这个吗?

预先非常感谢&此致。

I have a TabBar in my application and I do this in my AppDelegate:

...
test2ViewController = [[Test1ViewController alloc] init];
...
navigationTest2Controller = [[UINavigationController alloc] initWithRootViewController:test2ViewController];

NSArray *myControllers = [NSArray arrayWithObjects:..., navigationTest2Controller, nil];
[self.myTabBarController setViewControllers:myControllers animated:NO];

Now I have the problem that I am in a ViewController and I want to switch to the "navigationTest2Controller". I do this in my AppDelegate with:

self.myTabBarController.selectedViewController = navigationTest2Controller;

This works. It switches to this ViewController! This ViewController was already loaded and the viewDidLoad method was called. In this viewDidLoad method is a methos call:

[self myMethod];

I want, that if the view switches to this ViewController this "myMethod" should always be called.
How can I do this? In my AppDelegate before the line

self.myTabBarController.selectedViewController = navigationTest2Controller;

??? Or is there another delegate which will be called every time the ViewController is selected/switched to?

Does anyone know this?

Thanks a lot in advance & Best Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

多孤肩上扛 2024-08-24 11:36:10

您应该将视图可见时要运行的任何代码放置在 -viewWillAppear:-viewDidAppear 视图控制器的方法。

编辑:要使这种情况发生当您从某个视图切换时,您可以子类化UITabBarController,仅修改< code>–tabBarController:shouldSelectViewController: 方法。在该方法中,您可以执行以下操作:

- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController
{
    if (self.selectedIndex == 1 &&
        [viewController respondsToSelector:@selector(myMethod)]) {
        [viewController myMethod];
    }

    return YES;
}

You should place any code that you want to run when the view becomes visible in the -viewWillAppear: or -viewDidAppear method of your view controller.

EDIT: To make this happen only when you switch from a certain view, you can subclass UITabBarController, only modifying the –tabBarController:shouldSelectViewController: method. In that method, you could do something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController
{
    if (self.selectedIndex == 1 &&
        [viewController respondsToSelector:@selector(myMethod)]) {
        [viewController myMethod];
    }

    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文