项目的结构目录
image.png
项目开始
ViewController.swift
修改继承的基类
import UIKit
class ViewController: WBMainViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
WBMainViewController.swift
//
// WBMainViewController.swift
// SinoDemo
//
// Created by jiangbin on 2019/12/25.
// Copyright © 2019 ice. All rights reserved.
//
import UIKit
class WBMainViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
setupChildControllers()
}
}
extension WBMainViewController{
private func setupChildControllers () {
let arr = [
["clsName" : "WBHomeViewController" ,"title" : "首页" , "imageName" : "zzz"]
]
var arrM = [UIViewController]()
for dict in arr{
let vc = childController(dict: dict)
arrM.append(vc)
}
viewControllers = arrM
= }
private func childController (dict : [String : String]) -> UIViewController {
guard let clsName = dict["clsName"] ,
let title = dict["title"],
let imageName = dict["imageName"],
let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
else {
return UIViewController()
}
let vc = cls.init()
vc.title = title
vc.tabBarItem.title = title
vc.tabBarItem.image = UIImage(named: imageName)
let nav = WBNavigationViewController(rootViewController: vc)
return nav
}
}
网友评论