系统的,就是好。 解耦 性好。系统的 最稳定, 感觉。
老师的 真是 炫耀技巧, 造轮子。 效果还不好。 tabBar 之间 切换, 闪呀闪的。
老师造的轮子, 提供了 另一种 理解 的 思路, 理解系统 是 怎么实现的。
MainTabBarViewController
class MainTabBarViewController: UITabBarController {
func createViewControllers(){
let arrayNames = ["HomePageViewController","FindViewController","ChefViewController","FairViewController","ProfileViewController"]
let names = ["首页","发现","","订单","我"]
let imageNames = ["dock_home_unselected",
"dock_shop_unselected",
"custom_tabbar",
"dock_order_unselected",
"dock_self_unselected"]
let selectedImageNames = ["dock_home_selected",
"dock_shop_selected",
"custom_tabbar",
"dock_order_selected",
"dock_self_selected"]
var array = Array<UINavigationController>()
for i in 0..<arrayNames.count{
let controllerName = "deliciousOfCource." + arrayNames[i]
let viewControllerClass = NSClassFromString(controllerName) as! UIViewController.Type
let viewController = viewControllerClass.init()
viewController.tabBarItem.title = names[i]
viewController.tabBarItem.image = UIImage(named: imageNames[i])
viewController.tabBarItem.selectedImage = UIImage(named: selectedImageNames[i])
let navigationViewController = UINavigationController(rootViewController: viewController)
array.append(navigationViewController)
}
self.viewControllers = array
}
override func viewDidLoad() {
super.viewDidLoad()
createViewControllers()
self.tabBar.tintColor = UIColor.redColor()
}
KTCHomeViewController
class KTCHomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let appDelegation = UIApplication.sharedApplication().delegate as! AppDelegate
let control = appDelegation.window?.rootViewController
if control?.isKindOfClass(MainTabBarViewController.self) == true {
let mainTabBarCtrl = control as! MainTabBarViewController
mainTabBarCtrl.tabBar.hidden = false
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let appDelegation = UIApplication.sharedApplication().delegate as! AppDelegate
let control = appDelegation.window?.rootViewController
if control?.isKindOfClass(MainTabBarViewController.self) == true {
let mainTabBarCtrl = control as! MainTabBarViewController
mainTabBarCtrl.tabBar.hidden = false
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
let appDelegation = UIApplication.sharedApplication().delegate as! AppDelegate
let control = appDelegation.window?.rootViewController
if control?.isKindOfClass(MainTabBarViewController.self) == true {
let mainTabBarCtrl = control as! MainTabBarViewController
mainTabBarCtrl.tabBar.hidden = true
}
}