具有底部工具栏的 UINavigation 控制器在视图切换时丢失 UIBarButtonItems
我有一个以编程方式创建的导航控制器来处理 UISegmentedControl 视图之间的切换。 (称为segmentsNavigationController)
导航控制器有一个底部工具栏,其中有几个也以编程方式添加的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 都会丢失。这是为什么?我做错了什么吗?
I have a navigation controller created programmatically to handle the switch between the views of a UISegmentedControl. (called segmentsNavigationController)
The navigation controller has a bottom toolbar with a couple of UIBarButtonItems that have been added programmatically as well. In order to handle the switch I have the following piece of code:
- (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];
}
}
So eventually when I switch from one view to the other and back all my UIBarButtons in the bottom bar of the navigation controller are lost. Why is that? Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番搜索后,我意识到我使用 UINavigationController 的方式是错误的。
导航控制器的工具栏是内部显示的导航视图的一部分,而不是父视图的一部分。 (我花了一段时间才弄清楚这一点!)我在创建segmentsNavigationController的同一个控制器中创建按钮,而不是在我应该拥有的oneViewController内。
将代码移至 OneViewController 并将工具栏设置为:
成功了!
Well after some searching I came to realize that I've been using the UINavigationController the wrong way.
The toolbar of the navigation controller is part of the navigation view displayed inside and not of the parent. (It took me a while to figure this out!) I was creating the buttons in the same controller I was creating the segmentsNavigationController and not inside the oneViewController where I should have.
Moving the code to OneViewController and setting the toolbar as:
did the trick!