UITabBarController
有[首页
, 分类
, 购物车
, 我的
]页面
跳转需求:
点击购物车
需要判断是否已登录,未登录就present
登录
页面,登录后跳到购物车
页面
方法:
自定义UITabBarController
:BaseTabBarVC
-
在代理
tabBarController:shouldSelect:
拦截事件
判断:点击购物车
+ 是否登录,满足条件就present
登录
页面 -
重写
viewDidAppear
当present
的登录
页面dismiss
时,在viewDidAppear
判断是否已登录
class BaseTabBarVC: UITabBarController {
///点击tabbar'购物车'需要登录,登录dismiss后 跳转到'购物车'
fileprivate var loginBackSelectedTuple: (index: Int, shouldCompare: Bool) = (0, false)
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
///默认选中第n个tab
selectedIndex = 0
delegate = self
}
///当present的loginVC dismiss时tabbar调用viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if loginBackSelectedTuple.shouldCompare, extIsLogin() {
selectedIndex = loginBackSelectedTuple.index
loginBackSelectedTuple.shouldCompare = false
}
}
}
extension BaseTabBarVC: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard let vc = (viewController as? BaseNavVC)?.topViewController else { return false }
///未登录 + 购物车
if vc.clsName == "CartVC", !extIsLogin() {
///登录
extPresentLoginVC()
///记录登录后需要选中的selectedIndex
guard let vcs = viewControllers else { return false }
let selectedIndex = (vcs as NSArray).index(of: viewController)
loginBackSelectedTuple = (selectedIndex, true)
return false
}
return true
}
}
网友评论