ios - 如何快速防止 UIViewController 的多次推送?
问题描述
当我在下一个按钮上快速点击多次时,控制器会更清晰地堆叠多次,问题是我们需要多次单击后退按钮才能到达前一个屏幕。
下一个按钮代码
guard let controller = UIStoryboard(name: "Filepreview", bundle: nil).instantiateViewController(withIdentifier: "FilepreviewVC") as? PreviewFileViewController else {return}
self.navigationController?.pushViewController(controller, animated: true)
解决方案
不允许用户多次按下按钮!
这可以通过在按下新 VC 之前禁用按钮来完成:
nextButton.isEnabled = false
如果您不希望用户看到按钮被禁用,或者推送的触发器不是可以禁用的,只需使用私有属性来跟踪您自己的“禁用”状态:
private var nextButtonDisabled = false
// ...
// when you want to push the new VC, check nextButtonDisabled first!
if nextButtonDisabled {
return
}
nextButtonDisabled = false
// push new VC here
当用户导航回来时,viewDidAppear
将被调用,因此您可以启用nextButton
in viewDidAppear
:
nextButton.isEnabled = true
// or
nextButtonDisabled = false
或者,在navigationController(_:didShow:animated:)
委托方法中执行此操作。
self.navigationController?.delegate = self
// ...
extension MyViewController : UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if viewController == self {
nextButtonDisabled = false
}
}
}
推荐阅读
- php - 是否可以覆盖 Wordpress 中网站标题的回显功能?
- split - H2o Flow UI:Split Frame 如何适用于多类数据集?
- elixir - 在创建新的 Mix 项目期间,“主模块”的目的是什么?
- python - 如何将一个矩形分成 4 个部分?
- vb.net - 如何计算 vb.net 中使用或未使用的函数和方法?
- php - Laravel 5.8 不尊重作业类中指定的 $retryAfter
- python - 如何使用python和openpyxl在excel中搜索特定的列名(不是A、B等),例如名称、标记?
- ios - 这个控制台警告对我的约束意味着什么?
- android - 反应本机 USB 打印机库不工作
- google-sheets - 谷歌表格一个标准具有不同和多个结果