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

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

问题描述

我有一个 uitabbarcontroller,它包含多个选项卡和视图控制器.我正在尝试遍历视图控制器以找到正确的控制器并调用方法.但是我得到的视图控制器的类型,每次我通过循环时都是一个 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 的根目录下有 UINavigationController,因此您要做的是访问 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天全站免登陆