iOS 第二次点击TabBar刷新界面操作

1.在UITabBarController中设置delegate
2.实现方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

3.最终代码

OC:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([tabBarController.viewControllers containsObject:tabBarController.selectedViewController]) {
        if ([viewController isEqual:tabBarController.selectedViewController]) {
            NSLog(@"这里执行操作");
            return NO;
        }
    }
    return YES;
}

Swift:

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController == tabBarController.selectedViewController {
            let nav = viewController as! UINavigationController
            if let topvc = nav.topViewController{
                if topvc is VC {
                    print("在这里操作(oc那个写法 也可以按swift的来,懒得改了。。。。)")
                }
            }
            return false
        }
        return true
    }

你可能感兴趣的:(iOS 第二次点击TabBar刷新界面操作)