UITabbarController记录上一个选择的ViewController

最近在使用swift做项目,因此使用了swift语言,OC的话可以自行改写。
在tabbarController中加入以下方法

var array: [Int] = [0,]

func setSelectedVC() {
        
        let index = array[array.count-1]
        
        tabBarController?.selectedViewController = tabBarController?.viewControllers?[index]
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        let index = tabBar.items?.index(of: item)
        
        if index != selectedIndex {
            
            array.append(index!)
        }
    }

func backToLastVC() {

        let tc = tabBarController as! MTTabbarController
        
        let index = tc.array[tc.array.count-2]
        
        tabBarController?.selectedViewController = tabBarController?.viewControllers?[index]
    }

本人自己整理的方法,写得不好之处,希望大家能提出意见。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容