Swift模拟从服务区端加载指定的控制器类型

//
//  MainTableBarControllerViewController.swift
//  WeiboSwift
//
//  Created by hq on 16/6/6.
//  copyright © 2016年 hanqing. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addViewController()
        
    }
    
    //添加我们的tabbar控制器
    private func addViewController(){
        
        //模拟从远程加载我们控制器配置信息
        let jsonPath=NSBundle.mainBundle().pathForResource("MainVCSettings.json",ofType: nil);
        
        if ((jsonPath) != nil){
            
            let jsonData=NSData(contentsOfFile: jsonPath!)
            
            do{
                //try 如果出错后,下边的代码不再执行,直接进入到catch
                //try! 如果程序出错,直接崩溃
                
                let jsonArray=try NSJSONSerialization.JSONObjectWithData(jsonData!,options: NSJSONReadingOptions.MutableContainers)
                
                //swift便利数组
                //必须明确数组类型[]表示数组,数组中每个元素key:value为String
                for dict in jsonArray as! [[String:String]]{
                    
                    addOneVC(dict["vcName"]!,img: dict["imageName"]!,selectedImg: dict["selImageName"]!,title: dict["title"]!)
                }
                
            }catch{
                
                print(error)
                //如果出错了,使用本地认
                
                addOneVC("HomeViewController",img: "tabbar_home",selectedImg: "tabbar_home_highlighted",title: "首页")
                
                addOneVC("MessageViewController",img: "tabbar_message_center",selectedImg: "tabbar_message_center_highlighted",title: "消息")
                
                addOneVC("discoverViewController",img: "tabbar_discover",selectedImg: "tabbar_discover_highlighted",title: "发现")
                
                addOneVC("ProfileViewController",img: "tabbar_profile",selectedImg: "tabbar_profile_highlighted",title: "我的")
                
            }
        }


    }
    
    //添加一个导航控制器
    private func addOneVC(vcName: String,img: String,selectedImg: String,title: String ){
        
        //获取Info.plist项目的命名空间,并转换为字符串
       let nameSpace=NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
       
       //拼接命名空间,并将命名空间转换为我们的类
       let cla:AnyClass?=NSClassFromString(nameSpace + "." + vcName)
        
       let vccla=cla as! UIViewController.Type
        
       //创建我们的类
       let vc=vccla.init()
        
        
       vc.tabBarItem.image=UIImage(named: img)
        
       vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.grayColor()],forState: UIControlState.normal)
        
       vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()],forState: UIControlState.Selected)
        
       vc.tabBarItem.selectedImage=UIImage(named: selectedImg)
        
       vc.title=title;
        
       let nav=UINavigationController(rootViewController: vc)
        
       nav.navigationBar.titleTextAttributes=[NSForegroundColorAttributeName : UIColor.blackColor(),NSFontAttributeName:UIFont.systemFontOfSize(16)]
        
       addChildViewController(nav)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }
    

}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...