imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

UITabBarController UITabBar 相关 #96

Open imzyf opened 6 years ago

imzyf commented 6 years ago

第二次点当前 uitabbar 刷新当前数据,发通知

// 在 TabBarController 的基类
extension BaseTabBarController: UITabBarControllerDelegate {

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        // 已经被选中的 vc
        let selectedViewController = tabBarController.selectedViewController
        // 选中的 vc,本例需求是第二个
        if let viewControllers = tabBarController.viewControllers, viewControllers.count > 2, selectedViewController == viewControllers[1] {
            // 第二次选中
            if viewController == selectedViewController {
                // 通知
                let notificationName = Notification.Name(rawValue: "refresh")
                NotificationCenter.default.post(name: notificationName, object: nil)
                return false
            }
        }
        return true
    }
}

在 viewDidLoad() 添加 delegate = self