Swift之實(shí)現(xiàn)點(diǎn)擊UITabBarItem的任意跳轉(zhuǎn)

我們在使用UITabBarController時經(jīng)常會遇到這種情況:某個頁面在用戶沒有登陸的情況下,點(diǎn)擊item需要跳轉(zhuǎn)到登錄頁面。還可能會要求登錄頁面從當(dāng)前的item自下向上顯示。其實(shí)就是用到了UITabBarControllerDelegate中的一個代理方法。

1.下面開始實(shí)現(xiàn),首先我們創(chuàng)建一個MainTabBarController,實(shí)現(xiàn)效果:
MainTabBarViewController.png
2.我們定義一個全局的Bool變量:
  //定義一個變量,控制如何跳轉(zhuǎn)
  var change: Bool = true

當(dāng)然,這個變量主要是控制是要UITabBab點(diǎn)擊后是不是要按常理顯示。O(∩_∩)O哈!

再來一個變量,記錄一下上一次點(diǎn)擊的index:
//記錄上一次的index
var _lastSelectedIndex: NSInteger!
var lastSelectedIndex: NSInteger {
    if _lastSelectedIndex == nil {
        _lastSelectedIndex = NSInteger()
        //判斷是否相等,不同才設(shè)置
        if (self.selectedIndex != selectedIndex) {
            //設(shè)置最近一次
            _lastSelectedIndex = self.selectedIndex;
        }
        //調(diào)用父類的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 {
        //設(shè)置最近一次變更
        _lastSelectedIndex = self.selectedIndex
    }
}
下面我們實(shí)現(xiàn)代理方法:
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,我們可以在返回按鈕的響應(yīng)事件里面設(shè)置去哪個頁面(想去哪兒去哪兒,想干嘛干嘛)

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)
    }      
}

如果不設(shè)置的話,就是從哪來,回哪去。。。

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

效果就是這樣:


MainTabBarController.gif

MainTabBarController的完整代碼:

import UIKit

//定義一個變量,控制如何跳轉(zhuǎn)
var change: Bool = true

class MainTabBarController: UITabBarController , UITabBarControllerDelegate {

deinit {
    print("MainTabBarController銷毀")
}
//MARK: --life cyle
override func viewDidLoad() {
    super.viewDidLoad()
    
    // 添加跳轉(zhuǎn)到首頁的觀察者
    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
    
}
//跳轉(zhuǎn)到首頁
func pushHome(){
    self.selectedIndex = 0
}
//跳轉(zhuǎn)到收藏
func pushCollection() {
    self.selectedIndex = 2
}

//MARK: --setter getter
var _lastSelectedIndex: NSInteger!
var lastSelectedIndex: NSInteger {
    if _lastSelectedIndex == nil {
        _lastSelectedIndex = NSInteger()
        //判斷是否相等,不同才設(shè)置
        if (self.selectedIndex != selectedIndex) {
            //設(shè)置最近一次
            _lastSelectedIndex = self.selectedIndex;
        }
        //調(diào)用父類的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 {
        //設(shè)置最近一次變更
        _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
  }
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • 個性這個詞的概念從來沒有思考過,也從來沒有清晰、準(zhǔn)確的含義。因此探究明白這詞很有意義,個性受到那些影響因素呢?(出...
    秦家炎閱讀 257評論 0 1
  • 我想要擁有一棟房子 里面有兩個人 我和 MR.right 屋里有滿柜子書 屋外有一園子的花。 當(dāng)我醒來并不會驚...
    H0lly閱讀 216評論 0 0
  • 銀龍 2016.09.29 念往事成城,寂寞如雪,煙沙千里云橫 從來聚散都無限, 一場笑中夢浮生,簾外月迷蒙 想十...
    醉仙王子閱讀 257評論 0 1

友情鏈接更多精彩內(nèi)容