加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:55:27 所属栏目:百科 来源:网络整理
导读://// MainTableBarControllerViewController.swift// WeiboSwift//// Created by hq on 16/6/6.// Copyright ? 2016年 hanqing. All rights reserved.//import UIKitclass MainTabBarController: UITabBarController { override func viewDidLoad() { super.
//
//  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()
       
    }
    

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读