gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:39:44 31 4
gpt4 key购买 nike

我有一个正在做一些计算的 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com