防止在我按下已选择的选项卡时屏幕发生变化。

huangapple go评论60阅读模式
英文:

Prevent screen change when I press tab which is already selected

问题

我有一个包含4个选项卡的CustomTabBarController。当我在第3个选项卡内时,假设我有一个名为A的屏幕,上面有一个按钮,点击它会跳转到第二个名为B的屏幕。所以,当我在屏幕B上,按下第3个选项卡时,我想保持在屏幕B上。当前应用的行为是,当我在屏幕B上,按下第3个选项卡时,应用程序会导航到屏幕A。

您知道如何解决这个问题吗?

英文:

I have a CustomTabBarController with 4 tabs. When I am inside the 3rd tab, let's say I have a screen called A and I have a button which takes me a second screen called B. So, when I am on screen B and I am pressing the 3rd tab, I want to stay on screen B. The current app behaviour is when I am on screen B and I am pressing the 3rd tab, the app navigates to screen A.

Do you know how to solve this issue?

答案1

得分: 1

在您的CustomTabBarController子类中,继承UITabBarControllerDelegate协议,并在viewDidLoad()方法中将您的控制器的委托设置为self。然后覆盖func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool方法,如果当前的selectedViewController等于传入的视图控制器,则返回false

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if selectedViewController == viewController {
            return false
        }
        return true
    }
    
}
英文:

In your CustomTabBarController subclass inherit the UITabBarControllerDelegate protocol and in your viewDidLoad() set your controller's delegate to self. Then override the func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool method returning false if the current selectedViewController equals the passed in view controller.

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.delegate = self
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if selectedViewController == viewController {
            return false
        }
        return true
    }
    
}

huangapple
  • 本文由 发表于 2023年6月8日 20:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定