【问题标题】:iOS navigation issue: after pushing a ViewController, it results in a navigation bar showing the navigation items of the previous ViewControlleriOS 导航问题:推送 ViewController 后,会导致导航栏显示上一个 ViewController 的导航项
【发布时间】:2014-01-24 10:48:49
【问题描述】:

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

ViewController A 中,如果我按ViewController Bin,有时(并非总是)当ViewController B 确实出现时,导航栏会显示ViewController A 的导航栏项目,而不是ViewController B 的导航栏项目。如果我点击返回按钮,我无法返回到ViewController A,卡在ViewController B

ViewController A的导航项增加了UIBarButtonItemViewController 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;

        }

    }

}

有人知道吗?谢谢。

【问题讨论】:

  • 您是如何在 ViewControllerA 中添加条形按钮的??
  • 是的,A的导航项中增加了一个UIBarButtonItem,A的导航项会响应一些事件而更新。
  • 我的意思是你是否在 ViewController A 本身中添加了条形按钮??
  • 是的,我在ViewController A的bar右边添加了一个UIBarButtonItem

标签: ios objective-c uinavigationcontroller uinavigationbar uinavigationitem


【解决方案1】:

我终于找到了这个问题的原因。是我试图隐藏viewWillDisappear中的导航栏。现在 iOS 允许我通过从屏幕的左边缘平移来返回上一个视图。但是从左边缘平移的时候,如果我取消这个动作,再平移回到左边缘,导航栏就会进入这个奇怪的状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多