Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift模拟从服务区端加载指定的控制器类型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

// // MainTableBarControllerViewController.swift // WeiboSwift // // Created by hq on 16/6/6. // Copyright © 2016年 hanqing. All rights reserved. // import UIKit class MainTabBarController: UITab
//
//  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()
        
    }
    
    //@L_618_0@我们的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: "我的")
                
            }
        }


    }
    
    //@L_618_0@一个导航控制器
    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模拟从服务区端加载指定的控制器类型全部内容,希望文章能够帮你解决Swift模拟从服务区端加载指定的控制器类型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。