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

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

概述//// MainTableBarControllerViewController.swift// WeiboSwift//// Created by hq on 16/6/6.// Copyright © 2016年 hanqing. All rights reserved.//import UIKitclass MainTabBarController: UITab
////  MaintablebarControllerVIEwController.swift//  WeiboSwift////  Created by hq on 16/6/6.//  copyright © 2016年 hanqing. All rights reserved.//import UIKitclass 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模拟服务区端加载指定的控制器类型全部内容,希望文章能够帮你解决Swift模拟从服务区端加载指定的控制器类型所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1075236.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存