iOS导航问题:推送ViewController后,会生成一个导航栏,显示上一个ViewController的导航项

时间:2014-01-24 10:48:49

标签: ios objective-c uinavigationcontroller uinavigationbar uinavigationitem

这种情况很少发生,我不知道如何重现它,但它确实有时会发生,我想解决它。

ViewController A中,如果我推送ViewController B,有时(并非总是)出现ViewController B,导航栏会显示ViewController A的导航栏项,不是ViewController B的那些。如果我点击后退按钮,我就无法返回ViewController A,卡在ViewController B

UIBarButtonItem的{​​{1}}添加到ViewController A的导航项中,ViewController A的导航项将会更新,以响应某些事件。是导致这个问题的原因吗?

推送ViewController B

的代码
ViewControllerB* viewControllerB = [ViewControllerB new];
[self.navigationController pushViewController:viewControllerB animated:YES];

更新ViewController A

中导航项的代码
- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    if(NumberOfCalendarOperations == 0){
        [self showNavigatorBarButtons];
    }
    else{
        [self startRefreshAnimationOnUpdateButton];
    }
}



//This method is triggered through notification when the number of operations in   calendarOperationQueue is changed

-(void)calendarOperationQueueStateDidChange:(NSNotification*)notification{
    if(NumberOfCalendarOperations == 0){

        if (self.isShowActivityIndicator) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showNavigatorBarButtons];
            });
        }
    }
    else{
        if (!self.isShowActivityIndicator) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self startRefreshAnimationOnUpdateButton];
            });
        }
    }
}

/ **  *显示导航栏右侧的更新按钮  * /

-(void)showNavigatorBarButtons{
    self.isShowActivityIndicator = NO;
    UIBarButtonItem *updateButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sync.png"] style:UIBarButtonItemStylePlain target:self action:@selector(updateButtonDidPress:)];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:updateButton, nil]];}

/ **  *在更新按钮上开始刷新动画  * /

-(void)startRefreshAnimationOnUpdateButton{

    if (self.navigationController.topViewController != self) {
        return;
    }
    self.isShowActivityIndicator = YES;

    UIView* updateButtonView = nil;

    for (UIView *subViewInNavigationBar in self.navigationController.navigationBar.subviews){

    NSString *subViewClassAsString = NSStringFromClass([subViewInNavigationBar class]);

        if ([subViewClassAsString compare:@"UINavigationButton" /* TODO: be careful with this */
                              options:NSCaseInsensitiveSearch] == NSOrderedSame){

        if ([subViewInNavigationBar isKindOfClass:[UIView class]] == YES){

                if(updateButtonView == nil){
                    updateButtonView = subViewInNavigationBar;
                }
                else if(subViewInNavigationBar.center.x < updateButtonView.center.x){
                    updateButtonView = subViewInNavigationBar;
                }

        }

        }

    }

    for (UIView *subViewsInButton in updateButtonView.subviews){

        if ([subViewsInButton isKindOfClass:[UIImageView class]] == YES &&
            subViewsInButton.frame.origin.x != 0.0f &&
            subViewsInButton.frame.origin.y != 0.0f){

            [subViewsInButton removeFromSuperview];


            CGRect activityIndicatorFrame = self.updateButtonActivityIndicator.frame;
            activityIndicatorFrame.origin.x = (CGRectGetWidth(updateButtonView.frame) / 2.0f) - (CGRectGetWidth(activityIndicatorFrame) / 2.0f);
            activityIndicatorFrame.origin.y = (CGRectGetHeight(updateButtonView.frame) / 2.0f) - (CGRectGetHeight(activityIndicatorFrame) / 2.0f);
            self.updateButtonActivityIndicator.frame = activityIndicatorFrame;

            [self.updateButtonActivityIndicator startAnimating];

            [updateButtonView addSubview:self.updateButtonActivityIndicator];

            return;

        }

    }

}

任何人都有线索?谢谢。

1 个答案:

答案 0 :(得分:1)

最后我找到了这个问题的原因。我尝试隐藏viewWillDisappear中的导航栏。现在iOS允许我通过从屏幕左边缘平移来回到上一个视图。但是当从左边缘平移时,如果我取消此操作并向后平移到左边缘,导航栏将进入这种奇怪的状态。