gpt4 book ai didi

ios - Swift - 防止 UIViewController 中的返回事件

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:38 24 4
gpt4 key购买 nike

我有一个关于取消从 UIViewController 中的后退按钮触发的后退事件的问题。在 Objective-C 中有以下 extension .我真的不知道如何将它转换为 swift。我到目前为止尝试的是用我自己的函数覆盖 backBarButton,但它不起作用:

    navigation.backBarButtonItem?.action = #selector(MyController.back)
navigation.backBarButtonItem?.target = self

我搜索了诸如委托(delegate)函数之类的东西,但找不到与 backButton 相关的任何东西。

最佳答案

当我遇到这个问题时,我重写了这个 extension到 swift 3

此解决方案将系统后退按钮保留为“<”

public protocol VCWithBackButtonHandler {
func shouldPopOnBackButton() -> Bool
}

extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {

if viewControllers.count < (navigationBar.items?.count) ?? 0 {
return true
}

var shouldPop = true
let vc = self.topViewController

if let vc = vc as? VCWithBackButtonHandler {
shouldPop = vc.shouldPopOnBackButton()
}

if shouldPop {
DispatchQueue.main.async {[weak self] in
_ = self?.popViewController(animated: true)
}
} else {
for subView in navigationBar.subviews {
if(0 < subView.alpha && subView.alpha < 1) {
UIView.animate(withDuration: 0.25, animations: {
subView.alpha = 1
})
}
}
}

return false
}
}

用法:

class ViewController: UIViewController,VCWithBackButtonHandler{
public func shouldPopOnBackButton() -> Bool {
return false
}
}

关于ios - Swift - 防止 UIViewController 中的返回事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43584340/

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