如何禁用当前视图中显示的 backBarButtonItem?
将视图推送到 UINavigationController 堆栈并显示后,我需要在某些情况下禁用“后退”按钮,例如,当在该屏幕上编辑数据时。
以下隐藏了“后退”按钮
self.navigationItem.hidesBackButton=YES;
,但我需要禁用它。
还有其他几个关于如何隐藏它或建议首先不显示它的答案,但这些不是我需要实现的。
After a view has been pushed onto the UINavigationController stack and is displayed I need to disable the Back button under certain circumstances, e.g., when data is being edited on that screen.
The following hides the Back button
self.navigationItem.hidesBackButton=YES;
but I need to disable it.
There are several other answers about how to hide it or suggest not displaying it in the first place, but these are not what I need to implement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为 navigationController 的导航栏委托实现委托方法之一。看一下
[UINavigationBarDelegate navigationBar:shouldPopItem:]
方法。从此方法返回 YES 或 NO 将启用或禁用后退按钮。
You could implement one of the delegate methods for your navigationController's navigation bar delegate. Take a look at the
[UINavigationBarDelegate navigationBar:shouldPopItem:]
method.Returning YES or NO from this method will enable or disable the back button.
您可以实现一个 leftBarButtonItem (不可见且无效果)
将其添加到当前视图中(self.navigationItem.leftBarButtonItem)
并将其删除以访问您的后退按钮。
You could implement one leftBarButtonItem (invisible and without effect)
add it into your current view (self.navigationItem.leftBarButtonItem)
and remove it to access your backButton.
如果您同意禁用整个导航栏,一个简单的解决方案是
当然,如果您想保持启用其他栏按钮,则这不起作用。
If you're fine that the whole navigation bar is disabled, an easy solution is
Of course this doesn't work if you have other bar buttons that you want to keep enabled.