arButtonItem添加到UITabBarControlle

arButtonItem添加到UITabBarControlle

本文介绍了以编程方式将UIBarButtonItem添加到UITabBarController内的所有导航视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构如下:


  • 查看

  • 标签栏控制器

    • 导航控制器

      • 查看控制器


      • 查看控制器


      • 查看控制器


      • 查看控制器


      • 查看控制器


      • 查看控制器

      以上控制器已在界面构建器中初始化。

      The above controllers have been initialised in interface builder.

      我要做的是向每个导航控制器添加一个正确的 UIBarButtonItem ,但没有取得任何成功。

      What I'm trying to do is add a right UIBarButtonItem to each navigation controller but not having any success.

      以下是我的尝试:

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          self.view.backgroundColor = [UIColor clearColor];
          self.view.opaque = NO;
          self.tabBarController.view.frame = self.view.bounds;
      
          NSArray *currentViewControllers = self.tabBarController.viewControllers;
          NSMutableArray *updatedViewControllers = [NSMutableArray array];
      
          for (int i=0; i<currentViewControllers.count; i++) {
              UINavigationController *tempNav = [[UINavigationController alloc]init];
              tempNav = [currentViewControllers objectAtIndex:i];
              UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc]
                                                initWithTitle:@"Done"
                                                style:UIBarButtonItemStyleBordered
                                                target:self
                                                action:@selector(dismissLibraryBrowser)];
              tempNav.navigationItem.rightBarButtonItem = dismissButton;
              [updatedViewControllers addObject:tempNav];
              [dismissButton release];
              [tempNav release];
      
              NSLog(@"Added controller number %d",i);
          }
      
          self.tabBarController.viewControllers = [NSArray arrayWithArray:updatedViewControllers];
      
          [self.view addSubview:tabBarController.view];
      }
      

      代码执行时没有任何错误,但按钮没有出现。我确定我在这里误解了一些东西。非常感谢一些指导。

      The code executes without any errors, but the button doesn't appear. I'm sure I've misunderstood something here. Would appreciate some guidance.

      推荐答案

      通过重新创建viewControllers和临时数组,你会稍微复杂化一些事情。你只需要操作从笔尖加载的对象

      You are over complicating things slightly with recreating viewControllers and temporary arrays. You just need to manipulate the objects loaded from the nib

      [self.tabBarController.viewControllers enumerateObjectsUsingBlock:^(UINavigationController *navigationController, NSUInteger idx, BOOL *stop) {
      
          UIViewController *rootViewController = [navigationController.viewControllers objectAtIndex:0];
      
          UIBarButtonItem *rightBarButtonItem  =
            [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                             style:UIBarButtonItemStyleBordered
                                            target:self
                                            action:@selector(dismissLibraryBrowser)];
      
          rootViewController.navigationItem.rightBarButtonItem = rightBarButtonItem;
      
       }];
      






      至于你的应用程序的结构 - 文档对于说

      所以我建议有一个看看重组一些东西,如果你只是偶尔需要它,为什么不考虑以模态方式呈现呢?

      So I would suggest having a look at restructuring some stuff, if you need it only occasionally why not consider presenting it modally?

      这篇关于以编程方式将UIBarButtonItem添加到UITabBarController内的所有导航视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:17