如何防止迅速推送UIViewController的多次操作?

时间:2020-11-07 09:56:38

标签: ios swift navigation

当我非常快地在下一个按钮上轻击多次,然后将控制器堆叠起来更清晰地多次按下时,问题是我们需要在后退按钮上多次单击才能降落在前一个屏幕上。

下一个按钮代码

@ManyToMany

3 个答案:

答案 0 :(得分:0)

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

这可以通过在按下新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中启用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
        }
    }
}

答案 1 :(得分:0)

子类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之前的最后一行。

答案 2 :(得分:0)

expenseTitle={item.expenseTitle}

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