ios - 在 UINavigationBar 中动态更改 backButton 的可见性

标签 ios navigationcontroller

我有一个正在做一些计算的 View ,我想在这段时间内隐藏后退按钮。

我用它来打开一个有后退按钮和取消按钮的新 Controller :

[self.navigationController pushViewController:calcController animated:YES];

这就是我开始计算的方式,全部在主线程中:

- (void)startSth {
    self.viewMode = modeRunning;
    [self updateButtons];
    [self performSelector:@selector(doSth) withObject:nil afterDelay:0.1];
}

- (void)doSth {
     ...
     self.viewMode = modeFinished;
     [self updateButtons];
}

这是切换按钮可见性的方法:

- (void)updateButtons {
    BOOL busy = (self.viewMode==modeRunning);
    self.navigationItem.hidesBackButton = busy; //back button
    self.navigationItem.rightBarButtonItem.enabled = !busy; //cancel button
}

问题:在第一次运行时它按预期工作,按钮被隐藏然后再次出现。 单击后退按钮时, View 会弹出。在第二次运行时,按钮从一开始就丢失了。方法 updateButtons 被调用两次,记录变量 self.viewMode 和 busy 显示正确的行为(首先是 1 然后是 0),因此显然对 hidesBackButton 的调用不起作用。

我还尝试了将 navigationItem.backButton 设置为 nil 的示例,使用自定义空按钮,调用 setNeedsDisplay 或 setHidesBackButton:animated:,但都没有成功。

有什么想法吗?

最佳答案

发现:设置一个空的 leftBarButton 会隐藏 backButton。也许有人有更清洁的解决方案?

if(busy)
    {
        [self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:[[UIView new] autorelease]] autorelease] animated:NO];
    }
    else
    {
        [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    }

关于ios - 在 UINavigationBar 中动态更改 backButton 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8619312/

相关文章:

javascript - 如何渲染类似于 images.google.com 的栏?

ios - 我怎样才能让我的图钉落在带有动画的 MKMapView 上(并且可能 - 比平时慢)

ios - Xcode 中的 iPhone 模拟器是否支持低功耗蓝牙?

iphone - 通过 UINavigationController 将整个 UIView 从一个 ViewController 传递到另一个 ViewController

iphone - Xcode - Storyboard - 导航 Controller

iphone - 导航 Controller 溢出

ios - ManagedObjectContext performBlock(AndWait)死锁

ios - 如何通过 Objective-C 中的程序消除不是我自己创建的 UIAlertView?

ios - 在 ios 中使用 opencv 用网格变换图像的特定区域

html - 导航栏无缘无故移动