UITabBarController - 如何访问视图控制器? [英] UITabBarController - How to access a view controller?

查看:154
本文介绍了UITabBarController - 如何访问视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个uitabbarcontroller,它包含多个标签和viewControllers。
我试图遍历视图控制器以找到正确的方法并调用方法。
但是我得到的视图控制器的类型,每次循环都是UINavigationController。那么我怎样才能简单地访问我的tabBar中的视图控制器?

I have a uitabbarcontroller, which contains multiple tabs and viewControllers. I am trying to loop through the view controllers to find the right one and call a method. but the type of the view controller i get, each time i go through the loop is a UINavigationController. So how can i simply access a view controller in my tabBar?

for (UIViewController *v in self.tabBar.viewControllers)
{
     if ([v isKindOfClass:[MyViewController class]])
     {
          MyViewController *myViewController = v;
          [v doSomething];
     }
}


推荐答案

你很可能在Tabs的根目录下有UINavigationControllers,所以你要做的就是访问UINavigationController显示的ViewController。

You most likely have UINavigationControllers at the root of your Tabs, so what you will want to do is access the ViewController displayed by the UINavigationController.

尝试将代码更改为以下内容:

Try changing the code to the following:

for (UIViewController *v in self.tabBar.viewControllers) {

     UIViewController *vc = v;

     if ([v isKindOfClass:[UINavigationController class]]) {
         vc = [v visibleViewController];
     }

     if ([vc isKindOfClass:[MyViewController class]]) {
          MyViewController *myViewController = vc;
          [vc doSomething];
     }
}

这篇关于UITabBarController - 如何访问视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆