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

前端之家收集整理的这篇文章主要介绍了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()
       
    }
    

}

猜你在找的Swift相关文章