gpt4 book ai didi

ios - swift ios tabBar didSelectItem 呈现登录屏幕

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:42 26 4
gpt4 key购买 nike

如果用户单击选项卡 2 或选项卡 3,我会尝试显示登录屏幕。

我尝试添加:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

if item.tag == 1 || item.tag == 2 {
if LocalStore.getToken() == nil {
self.performSegueWithIdentifier("loginSegue", sender: self)
return
}
}
}

这将显示模态 VC/登录屏幕,我从 rootVC 开始使用它。但是标签栏仍然会转到点击的标签。

我想做的是停止标签栏形式向选定的 VC/单击的标签触发 segue,而是只显示模态 VC/登录屏幕

最佳答案

如果您正在使用 UITabBarController,您可以覆盖其委托(delegate) (UITabBarControllerDelegate) 的 shouldSelectViewController 方法。您可以动态决定是否要切换到特定的 View Controller :

func tabBarController(tabBarController: UITabBarController,
shouldSelectViewController viewController: UIViewController) -> Bool {
guard
let tab = tabBarController.viewControllers?.indexOf(viewController)
where [1, 2].contains(tab)
else { return true }
if LocalStore.getToken() == nil {

/// Present the login screen here

return false
}
return true
}

关于ios - swift ios tabBar didSelectItem 呈现登录屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37364055/

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