思路
在Swift的代碼中堡牡,Controller沒(méi)有銷(xiāo)毀大部分的原因都是沒(méi)有weak self午绳。怎么檢測(cè)一個(gè)Controller沒(méi)有釋放呢筒溃?
1马篮、當(dāng)一個(gè)對(duì)象銷(xiāo)毀的時(shí)候,它會(huì)調(diào)用deinit的方法怜奖,一般Controller頁(yè)面我們都是放在UINavigationController里面的浑测,然后調(diào)用push和pop,實(shí)現(xiàn)我們頁(yè)面的跳轉(zhuǎn)歪玲。
2迁央、繼承UINavigationController怎顾,重寫(xiě)push方法,在push方法里面把push的controller的名字放到單例的數(shù)組里面漱贱,deinit的時(shí)候在把當(dāng)前的controller從單例里面釋放槐雾,然后檢測(cè)單例里面有沒(méi)有controller的相同名字存在2個(gè)以上的。
代碼實(shí)現(xiàn)
因?yàn)槭菣z測(cè)Controller有沒(méi)有銷(xiāo)毀的工具幅狮,然后打印到console里查看募强,所以代碼應(yīng)該在DEBUG模式下執(zhí)行。
如果使用的UI架構(gòu)師tabbar加幾個(gè)controllers的樣式崇摄,初始化tabbar的時(shí)候會(huì)調(diào)用push擎值。所以判斷如果是tabbarcontroller直接return
func defaultController() -> UINavigationController {
let navi = JYNavigationViewController.init(rootViewController: JYTabBarViewController())
return navi
}
func pushVc(_ vc: UIViewController) {
#if DEBUG
if vc is JYTabBarViewController {
return
}
vcs.append(NSStringFromClass(vc.classForCoder))
#endif
}
在繼承的NavigationController實(shí)現(xiàn)push代碼
override open func pushViewController(_ viewController: UIViewController, animated: Bool) {
viewController.hidesBottomBarWhenPushed = self.viewControllers.count > 0
if self.viewControllers.count > 0 {
viewController.showLeftButton()
}
CheckWselfHelper.shared.pushVc(viewController)
super.pushViewController(viewController, animated: animated)
}
在pop代碼里調(diào)用數(shù)組的filter函數(shù),過(guò)濾掉當(dāng)前controller名字相同的內(nèi)容逐抑,然后遍歷數(shù)組鸠儿,篩選出數(shù)組中名字相同有大于1個(gè)controller,并打印出來(lái)
func popVc(_ vc: UIViewController?) {
#if DEBUG
guard let vc = vc else {return}
let str = NSStringFromClass(vc.classForCoder)
vcs = vcs.filter({$0 != str})
var datas = [String: Int]()
vcs.forEach({ (str) in
datas[str] = (datas[str] ?? 0)+1
})
datas.forEach({ (key, value) in
if value > 1 {
Log.i("注意"+key+"沒(méi)有釋放")
}
})
#endif
}
在BaseController的deinit方法里實(shí)現(xiàn)我們的pop函數(shù)
deinit {
CheckWselfHelper.shared.popVc(self)
}
由于我是tabba的ui架構(gòu)厕氨,所以在點(diǎn)擊tabbar的時(shí)候會(huì)push很多tabbar和navigation的controller进每。所以在點(diǎn)擊和初始化的時(shí)候清空下我們的單例數(shù)組。
func clearVcs(_ addVc: UIViewController?) {
#if DEBUG
vcs.removeAll()
guard let addVc = addVc else {return}
pushVc(addVc)
#endif
}
在tabbar里面的實(shí)現(xiàn)如下
初始化的時(shí)候清空
func initViews() {
let tvc = JYTicketViewController()
let ticketVc: JYNavigationViewController = JYNavigationViewController(rootViewController: tvc)
let mallVc: JYNavigationViewController = JYNavigationViewController(rootViewController: JYMallViewController())
let mineVc: JYNavigationViewController = JYNavigationViewController(rootViewController: JYMineViewController())
ticketVc.tabBarItem.title = "購(gòu)票"
ticketVc.tabBarItem.image = UIImage.init(named: "ticket_normal")?.withRenderingMode(.alwaysOriginal)
ticketVc.tabBarItem.selectedImage = UIImage.init(named: "ticket_selected")?.withRenderingMode(.alwaysOriginal)
mallVc.tabBarItem.title = "商城"
mallVc.tabBarItem.image = UIImage.init(named: "mall_normal")?.withRenderingMode(.alwaysOriginal)
mallVc.tabBarItem.selectedImage = UIImage.init(named: "mall_selected")?.withRenderingMode(.alwaysOriginal)
mineVc.tabBarItem.title = "我的"
mineVc.tabBarItem.image = UIImage.init(named: "mine_normal")?.withRenderingMode(.alwaysOriginal)
mineVc.tabBarItem.selectedImage = UIImage.init(named: "mine_selected")?.withRenderingMode(.alwaysOriginal)
self.tabBar.barTintColor = UIColor.white
self.tabBar.tintColor = AppConfig.mainColor
let lists = [ticketVc, mallVc, mineVc]
self.viewControllers = lists
self.delegate = self
self.selectedIndex = 0
ticketVc.navigationBar.isTranslucent = false
mallVc.navigationBar.isTranslucent = false
mineVc.navigationBar.isTranslucent = false
CheckWselfHelper.shared.clearVcs(tvc)//這里初始化默認(rèn)選中的controller
}
點(diǎn)擊tabbar的時(shí)候清空
// MARK: UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
CheckWselfHelper.shared.clearVcs(viewController.childViewControllers.first)
return true
}
CheckWselfHelper.swift
https://gist.github.com/jackyshan/7a084291a03ae55815631697be1ae995