具有底部工具栏的 UINavigation 控制器在视图切换时丢失 UIBarButtonItems

发布于 2024-10-20 15:49:30 字数 1513 浏览 1 评论 0原文

我有一个以编程方式创建的导航控制器来处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-10-27 15:49:30

经过一番搜索后,我意识到我使用 UINavigationController 的方式是错误的。

导航控制器的工具栏是内部显示的导航视图的一部分,而不是父视图的一部分。 (我花了一段时间才弄清楚这一点!)我在创建segmentsNavigationController的同一个控制器中创建按钮,而不是在我应该拥有的oneViewController内。

将代码移至 OneViewController 并将工具栏设置为:

    [self setToolbarItems: [[NSArray alloc] initWithObjects: button1, button2, nil] animated: NO];

成功了!

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:

    [self setToolbarItems: [[NSArray alloc] initWithObjects: button1, button2, nil] animated: NO];

did the trick!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文