【问题标题】:How to prevent multiple push of UIViewController in swift?如何快速防止 UIViewController 的多次推送?
【发布时间】:2020-11-07 09:56:38
【问题描述】:

当我在下一个按钮上快速点击多次时,控制器会更清晰地堆叠多次,问题是我们需要多次点击后退按钮才能到达上一个屏幕。

下一个按钮代码

guard  let controller = UIStoryboard(name: "Filepreview", bundle: nil).instantiateViewController(withIdentifier: "FilepreviewVC") as? PreviewFileViewController else {return}
                            self.navigationController?.pushViewController(controller, animated: true)

【问题讨论】:

  • 如何在按下下一个按钮后禁用它,并仅在viewDidAppear 中启用它(或从推送的VC返回时将调用的其他方法)?
  • 我没有禁用按钮,实际上在表格视图行选择中也执行了相同的操作
  • 好吧,那你怎么办?或者至少,在 1 次按下后使其“不起作用”。
  • 在 viewWillAppear 上启用下一个按钮

标签: ios swift navigation


【解决方案1】:

不允许用户多次按下按钮!

这可以通过在你按下新的 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 会被调用,所以你可以在viewDidAppear 中启用nextButton

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
        }
    }
}

【讨论】:

  • 如何使用这个navigationController(_:didShow:animated:) ?
  • 实际上我必须通过表格视图行选择来实现这一点
  • @AJ 没问题,使用私有财产的方式。如果在didSelectRowAt 中为真,则立即return
【解决方案2】:

子类UINavigationController 并覆盖这个函数

public override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    if
        let currentTopVC = viewControllers.last as? PreviewFileViewController,
        previewFileVC = viewController as? PreviewFileViewController,
        currentTopVC.file.id == previewFileVC.file.id {
        return
    }

    super.pushViewController(viewController, animated: animated)
}

我在这里假设PreviewFileViewController 中有某种类型的身份超出了类型,因此如果它们预览不同的文件,可能会有两个实例相互堆叠。如果只能预览一次,您可以省略return 之前的最后一行。

【讨论】:

    【解决方案3】:
    @IBAction func Submit(_ sender: Any) {
        for _ in 0..<10 {
            DispatchQueue.main.async {
                guard self.navigationController?.viewControllers.contains(where: {$0.isKind(of: TestVC.self)}) == false else { return }
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "TestVC") as! TestVC
                vc.view.backgroundColor = self.generateRandomColor()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
    

    guard self.navigationController?.viewControllers.contains(where: {$0.isKind(of: TestVC.self)}) == false else { return } 使用它来检查视图控制器是否已经在堆栈中。

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多