以编程方式将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;
}];
至于你的应用程序的结构 - 文档对于 UITabBarController 说
部署标签栏界面时,必须将此视图安装为窗口的根目录。与其他视图控制器不同,标签栏界面不应该作为另一个视图控制器的子节点安装。
When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
所以我建议有一个看看重组一些东西,如果你只是偶尔需要它,为什么不考虑以模态方式呈现呢?
So I would suggest having a look at restructuring some stuff, if you need it only occasionally why not consider presenting it modally?