英文:
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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论