//
// DSTabBarController.swift
// DSWeibo
//
// Created by 刘小二 on 16/5/22.
// Copyright © 2016年 刘小二. All rights reserved.
//
import UIKit
class DSTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
addchildViewControllers()
}
private func addchildViewControllers()
{
// 设置全局的文字图片的选中颜色
tabBar.tintColor = UIColor.orangeColor()
// 添加子控制器
// let home = HomeViewController()
// addChildViewController(childController:home, title: "首页", image: "tabbar_home", selectedImage: "tabbar_home" + "highlighted")
// let discover = DicoverViewController()
// addChildViewController(childController:discover, title: "发现", image: "tabbar_discover", selectedImage: "tabbar_discover" + "highlighted")
// let message = MessageViewController()
// addChildViewController(childController:message, title: "消息", image: "tabbar_message_center", selectedImage: "tabbar_message_center" + "highlighted")
// let mine = MineViewController()
// addChildViewController(childController: mine, title: "我的", image: "tabbar_profile", selectedImage: "tabbar_profile" + "highlighted")
// 使用字符串创建对应的类
addChildViewController(childController: "HomeViewController", title: "首页", image: "tabbar_home", selectedImage: "tabbar_home" + "highlighted")
addChildViewController(childController: "DicoverViewController", title: "发现", image: "tabbar_discover", selectedImage: "tabbar_discover" + "highlighted" )
addChildViewController(childController: "MessageViewController", title: "消息", image: "tabbar_message_center", selectedImage: "tabbar_message_center" + "highlighted" )
addChildViewController(childController: "MineViewController", title: "我的", image: "tabbar_profile", selectedImage: "tabbar_profile" + "highlighted" )
}
}
extension DSTabBarController
{
// MARK: - 添加子控制器的方法
func addChildViewController(childController childController: String , title: String , image : String , selectedImage : String) {
// 得到命名空间
let mm = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
let vcc:AnyClass? = NSClassFromString(mm + "." + childController)
let vc = vcc as! UIViewController.Type
let oc = vc.init()
oc.tabBarItem.title = title
oc.tabBarItem.image = UIImage(named:image)
oc.tabBarItem.selectedImage = UIImage(named: selectedImage)
let nav = DSNavigationController(rootViewController:oc)
addChildViewController(nav)
}
}
如何通过字符串创建类 动态获取命名空间
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- Swift中用类名字符串创建类(用到了命名空间) OC中可以直接通过类名的字符串转换成对应的类来操作,但是Swif...
- 1. tips 1.1在swift中,类名的组成格式是 namespace.类名.比如我们在任意一个控制器的vie...
- 在swift开发中,也是通过用字符串创建类也是通过NSClassFromString函数进行的。 NSClassF...
- 大家好,我是尝胆人Frank,取自卧薪尝胆,字面意思是遍尝苦涩之物,引申为厚积薄发。 我今天跟大家分享的主题是: ...