我有一个以编程方式创建的导航控制器来处理UISegmentedControl视图之间的切换。 (称为segmentsNavigationController)
导航控制器有一个底部工具栏,其中包含一些UIBarButtonItems,这些UIBarButtonItems也是以编程方式添加的。为了处理交换机,我有以下代码:
- (void)indexDidChangeForSegmentedControl:(UISegmentedControl *) aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
if( index == 0 ) {
OneViewController *oneViewController = nil;
if( (oneViewController = [self.viewControllers objectForKey:@"one"]) == nil ) {
oneViewController = [[OneViewController alloc] init];
[self.viewControllers setObject:oneViewController forKey:@"one"];
[oneViewController release];
}
NSArray *theViewControllers = [NSArray arrayWithObject:oneViewController];
[self.segmentsNavigationController setViewControllers:theViewControllers animated:YES];
}
else if( index == 1 ) {
TwoViewController *twoViewController = nil;
if( (twoViewController = [self.viewControllers objectForKey:@"two"]) == nil ) {
twoViewController = [[RelatedArticlesViewController alloc] init];
[self.viewControllers setObject:twoViewController forKey:@"two"];
twoViewController.hidesBottomBarWhenPushed = YES;
[twoViewController release];
}
NSArray *theViewControllers = [NSArray arrayWithObject:twoViewController];
[self.segmentsNavigationController setViewControllers:theViewControllers animated:YES];
}
}
所以最终当我从一个视图切换到另一个视图并返回导航控制器底部栏中的所有UIBarButtons都丢失了。这是为什么?我做错了吗?
答案 0 :(得分:1)
经过一番搜索,我意识到我一直在以错误的方式使用UINavigationController。
导航控制器的工具栏是内部显示的导航视图的一部分,而不是父级的导航视图的一部分。 (我花了一些时间来解决这个问题!)我在创建segmentsNavigationController的同一控制器中创建了按钮,而不是在我应该拥有的oneViewController中。
将代码移动到OneViewController并将工具栏设置为:
[self setToolbarItems: [[NSArray alloc] initWithObjects: button1, button2, nil] animated: NO];
做了伎俩!