ios 从 UIVIewController 访问 UITabBarController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22807240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:53:17  来源:igfitidea点击:

Accessing UITabBarController from UIVIewController

iosobjective-ciphonexcodeuitabbar

提问by rustylepord

I am developing an application based on UITabbar and the view hierarchy as follows.

我正在开发一个基于 UITabbar 和视图层次结构的应用程序,如下所示。

UITabBarController ----> UINavigationController ----> UIViewController

UITabBarController ----> UINavigationController ----> UIViewController

I need to access the UITabBarController from the UIIVewController . But following properties always returns nil.

我需要从 UIIVewController 访问 UITabBarController 。但是以下属性总是返回 nil。

self.tabBarController and self.navigationController.tabBarController

self.tabBarController 和 self.navigationController.tabBarController

Is there a way to access the Tabbarcontroller directly from a child viewController without using the AppDelegate ?

有没有办法直接从子 viewController 访问 Tabbarcontroller 而不使用 AppDelegate ?

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Home";
        self.navigationItem.title = @"Home";

        self.tabBarItem.image = [UIImage imageNamed:@"TabBarHome"];

        UITabBarController *tab = self.tabBarController;
         UITabBarController *tab1 = self.navigationController.tabBarController;
        UITabBarController *tab2 = self.navigationController.presentingViewController;



    }
    return self;
}

回答by Gabriel.Massana

With the hierachy that you are using:

使用您正在使用的层次结构:

enter image description here

在此处输入图片说明

I can acces without problem the UITabBarControllerfrom the ViewControllerwith:

我可以毫无问题的存取权限UITabBarControllerViewController具有:

self.tabBarController

self.tabBarController

Move your Custom initialization to viewDidLoador viewDidAppear

将您的自定义初始化移动到viewDidLoadviewDidAppear

Then for shure you can access TabBarControllerwith self.tabBarController

那么对于舒尔,您可以访问TabBarControllerself.tabBarController

Another way to arrive to your TabBarController is:

到达 TabBarController 的另一种方法是:

UITabBarController *tabBarController = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;

But it is totally unnecessary in your case.

但在你的情况下完全没有必要。

EDIT:

编辑:

If you are working with Xib, then you has been created a TabBarController programmatically in your AppDelegate. I'm sure you have something like:

如果您正在使用 Xib,那么您已经在 AppDelegate 中以编程方式创建了一个 TabBarController。我相信你有这样的事情:

self.tabBarController = [[UITabBarController alloc] init];

self.tabBarController = [[UITabBarController alloc] init];

Then you can call it in your ViewController:

然后你可以在你的 ViewController 中调用它:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]
UITabBarController *tabBarController = appdelegate.tabBarController;

回答by Rashad

You are doing it wrong.

你做错了。

I've an app same as yours. I can access tabbar from viewDidLoad.

我有一个和你一样的应用程序。我可以从viewDidLoad.

Try this:

尝试这个:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBarController setSelectedIndex:1];
}

Hope this helps.. :)

希望这可以帮助.. :)