开发者

UINavigation controller with bottom toolbar loses UIBarButtonItems on view switch

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 cont开发者_运维技巧roller are lost. Why is that? Am I doing something wrong?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜