一、封装方法 创建子控制器
//调用封装方法
addChildViewController(LXRHomeViewController(), title: "首页", imageName: "tabbar_home")
addChildViewController(LXRMessageViewController(), title: "消息", imageName: "tabbar_message_center")
addChildViewController(LXRDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
addChildViewController(LXRProfileViewController(), title: "我", imageName: "tabbar_profile")
// Swift支持方法的重改:方法名称相同.但是参数类型和个数不同
// private在当前文件中可以访问,其他文件不能访问
// swift3.0 private建议修改为fileprivate
private func addChildViewController(_ childVc: UIViewController, title : String, imageName : String) {
//1.设置自控制器的属性
childVc.title = title
childVc.tabBarItem.image = UIImage(named: imageName)
childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
//2.包装导航控制器
let childNav = UINavigationController(rootViewController: childVc)
//3.添加控制器
addChildViewController(childNav)
}
二、根据Json文件,通过字符串获取数据
创建MainVCSettings.json的json文件,并导入项目中,文件内容如下:
[
{
"vcName": "HomeViewController",
"title": "首页",
"imageName": "tabbar_home"
},
{
"vcName": "MessageViewController",
"title": "消息",
"imageName": "tabbar_message_center"
},
{
"vcName": "DiscoverViewController",
"title": "广场",
"imageName": "tabbar_discover"
},
{
"vcName": "ProfileViewController",
"title": "我",
"imageName": "tabbar_profile"
}
]
获取不到文件路径请按照这篇文章配置下
http://www.jianshu.com/p/db888332206a
//1.获取JSON文件路径
guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
LXRLog(message: "没有获取到对应的文件路径")
return
}
//2.读取json文件中的内容
guard let jsonData = try? Data(contentsOf: URL(fileURLWithPath: jsonPath)) else {
LXRLog(message: "没有获取到json文件中数据")
return
}
//3.将Data转成数组
guard let anyObject = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) else {
return
}
guard let dictArray = anyObject as? [[String : AnyObject]] else{
return
}
//4.遍历字典,获取对应的信息
for dict in dictArray{
//4.1获取控制器的对应的字符串
guard let VcName = dict["vcName"] as? String else {
continue
}
//4.2获取控制器显示的title
guard let title = dict["title"] as? String else {
continue
}
//4.3获取控制器显示的图标名称
guard let imageName = dict["imageName"] as? String else {
continue
}
//4.4添加自控制器
addChildViewController(VcName, title: title, imageName: imageName)
}
/**************************************************/
// Swift支持方法的重改:方法名称相同.但是参数类型和个数不同
// private在当前文件中可以访问,其他文件不能访问
private func addChildViewController(_ childVcName: String, title : String, imageName : String) {
//0.获得命名空间
guard let nameSpace = (Bundle.main.infoDictionary!["CFBundleExecutable"] as? String) else {
LXRLog(message: "没有获取命名空间")
return
}
//1.根据字符创获取对应的Class
guard let ChildVcClass = NSClassFromString(nameSpace + "." + childVcName) else {
LXRLog(message: "没有获取到字符创对应的Class")
return
}
// 2.将对应的AnyObject转成控制器的类型
guard let childVcType = ChildVcClass as? UIViewController.Type else {
LXRLog(message: "没有获取对应控制器的类型")
return
}
// 3.创建对应的控制器对象
let childVc = childVcType.init()
//4.设置自控制器的属性
childVc.title = title
childVc.tabBarItem.image = UIImage(named: imageName)
childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
//5.包装导航控制器
let childNav = UINavigationController(rootViewController: childVc)
//6.添加控制器
addChildViewController(childNav)
}
网友评论