Swift之实现点击UITabBarItem的任意跳转

我们在使用UITabBarController时经常会遇到这种情况:某个页面在用户没有登陆的情况下,点击item需要跳转到登录页面。还可能会要求登录页面从当前的item自下向上显示。其实就是用到了UITabBarControllerDelegate中的一个代理方法。

1.下面开始实现,首先我们创建一个MainTabBarController,实现效果:
Swift之实现点击UITabBarItem的任意跳转_第1张图片
MainTabBarViewController.png
2.我们定义一个全局的Bool变量:
  //定义一个变量,控制如何跳转
  var change: Bool = true

当然,这个变量主要是控制是要UITabBab点击后是不是要按常理显示。O(∩_∩)O哈!

再来一个变量,记录一下上一次点击的index:
//记录上一次的index
var _lastSelectedIndex: NSInteger!
var lastSelectedIndex: NSInteger {
    if _lastSelectedIndex == nil {
        _lastSelectedIndex = NSInteger()
        //判断是否相等,不同才设置
        if (self.selectedIndex != selectedIndex) {
            //设置最近一次
            _lastSelectedIndex = self.selectedIndex;
        }
        //调用父类的setSelectedIndex
        super.selectedIndex = selectedIndex
    }
    return _lastSelectedIndex
}

 override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    //获取选中的item
    let tabIndex = tabBar.items?.index(of: item)
    if tabIndex != self.selectedIndex {
        //设置最近一次变更
        _lastSelectedIndex = self.selectedIndex
    }
}
下面我们实现代理方法:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController == self.viewControllers![2]  {
        if change {
            
            self.selectedIndex = _lastSelectedIndex
            let login = LoginViewController()
            login.tag = 2
            let nav = UINavigationController(rootViewController: login)
            self.viewControllers![selectedIndex].present(nav, animated: true, completion: nil)
            return false
        } else {
            return true
        }
    }
    return true
}

在controller,我们可以在返回按钮的响应事件里面设置去哪个页面(想去哪儿去哪儿,想干嘛干嘛)

func backClick(sender: UIBarButtonItem) {
    if tag == 2 {
       //不管从哪也来的,都回首页
       NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pushLogin"), object: self, userInfo: nil)
       self.dismiss(animated: true, completion: nil)
    }      
}

如果不设置的话,就是从哪来,回哪去。。。

func backClick(sender: UIBarButtonItem) {
    if tag == 2 {
        self.dismiss(animated: true, completion: nil)
    }
}

效果就是这样:


Swift之实现点击UITabBarItem的任意跳转_第2张图片
MainTabBarController.gif

MainTabBarController的完整代码:

import UIKit

//定义一个变量,控制如何跳转
var change: Bool = true

class MainTabBarController: UITabBarController , UITabBarControllerDelegate {

deinit {
    print("MainTabBarController销毁")
}
//MARK: --life cyle
override func viewDidLoad() {
    super.viewDidLoad()
    
    // 添加跳转到首页的观察者
    NotificationCenter.default.addObserver(self, selector: #selector(pushHome), name: NSNotification.Name(rawValue: "pushLogin"), object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(pushCollection), name: NSNotification.Name(rawValue: "pushCollection"), object: nil)
    
    self.tabBarController?.hidesBottomBarWhenPushed = true
    initBaseLayout()
    
    self.delegate = self
    
}

//MARK: - prinvate methods
//添加视图
func initBaseLayout(){
    //主页
    let homeVC = HomeViewController()
    let homeNav = UINavigationController(rootViewController: homeVC)
    let image1 = UIImage(named: "tabbaritem_home_normal")?.withRenderingMode(.alwaysOriginal)
    let image1_s = UIImage(named: "tabbaritem_home_select")?.withRenderingMode(.alwaysOriginal)
    let item1:UITabBarItem = UITabBarItem(title: "首页", image: image1, selectedImage: image1_s)
    item1.tag = 0
    homeNav.tabBarItem = item1
    
    //分类
    let classVC = ClassifyViewController()
    let classNav = UINavigationController(rootViewController: classVC)
    let image2 = UIImage(named: "tabbaritem_fenlei_normal")?.withRenderingMode(.alwaysOriginal)
    let image2_s = UIImage(named: "tabbaritem_fenlei_select")?.withRenderingMode(.alwaysOriginal)
    let item2:UITabBarItem = UITabBarItem(title: "分类", image: image2, selectedImage: image2_s)
    item2.tag = 1
    classNav.tabBarItem = item2
    
    //收藏
    let collectionVC = CollectionViewController()
    let collectionNav = UINavigationController(rootViewController: collectionVC)
    let image3 = UIImage(named: "tabbaritem_collect_normal")?.withRenderingMode(.alwaysOriginal)
    let image3_s = UIImage(named: "tabbaritem_collect_select")?.withRenderingMode(.alwaysOriginal)
    let item3:UITabBarItem = UITabBarItem(title: "收藏", image: image3, selectedImage: image3_s)
    item3.tag = 2
    collectionNav.tabBarItem = item3
    
    //我的
    let mineVC = MineViewController()
    let mineNav = UINavigationController(rootViewController: mineVC)
    let image4 = UIImage(named: "tabbaritem_mine_normal")?.withRenderingMode(.alwaysOriginal)
    let image4_s = UIImage(named: "tabbaritem_mine_select")?.withRenderingMode(.alwaysOriginal)
    let item4:UITabBarItem = UITabBarItem(title: "我的", image: image4, selectedImage: image4_s)
    item4.tag = 3
    mineNav.tabBarItem = item4
    
    let tabArray = [homeNav,classNav,collectionNav,mineNav]
    self.viewControllers = tabArray
    
}
//跳转到首页
func pushHome(){
    self.selectedIndex = 0
}
//跳转到收藏
func pushCollection() {
    self.selectedIndex = 2
}

//MARK: --setter getter
var _lastSelectedIndex: NSInteger!
var lastSelectedIndex: NSInteger {
    if _lastSelectedIndex == nil {
        _lastSelectedIndex = NSInteger()
        //判断是否相等,不同才设置
        if (self.selectedIndex != selectedIndex) {
            //设置最近一次
            _lastSelectedIndex = self.selectedIndex;
        }
        //调用父类的setSelectedIndex
        super.selectedIndex = selectedIndex
    }
    return _lastSelectedIndex
}


override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    //获取选中的item
    let tabIndex = tabBar.items?.index(of: item)
    if tabIndex != self.selectedIndex {
        //设置最近一次变更
        _lastSelectedIndex = self.selectedIndex        
    }
}

//MARK: -- UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController == self.viewControllers![2]  {
        if change {
            
            self.selectedIndex = _lastSelectedIndex
            let login = LoginViewController()
            login.tag = 2
            let nav = UINavigationController(rootViewController: login)
            self.viewControllers![selectedIndex].present(nav, animated: true, completion: nil)
            return false
        } else {
            return true
        }
    }
    return true
  }
 }

你可能感兴趣的:(Swift之实现点击UITabBarItem的任意跳转)