Swift纯代码构建项目框架

来源:互联网 发布:扫描文件编辑软件 编辑:程序博客网 时间:2024/04/29 18:08

Swift纯代码构建项目框架 (简单的一个搭建项目的框架结构)

AppDelegate.swift中代码 (主要是设置根控制器)

import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    // 设置全局颜色    UITabBar.appearance().tintColor = UIColor.orangeColor()    // 创建window    window = UIWindow(frame: UIScreen.mainScreen().bounds)    //  设置根控制器    window?.rootViewController = MainViewController()    window?.makeKeyAndVisible()    return true}}

MainViewController.swift 代码(设置Tabbar)类似微博 HomeViewController()都是控制器

import UIKitclass MainViewController: UITabBarController {override func viewDidLoad() {    super.viewDidLoad()    addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")    addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")    addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")    addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")}// swift支持方法的重载// 方法的重载:方法名称相同,但是参数不同. --> 1.参数的类型不同 2.参数的个数不同// private在当前文件中可以访问,但是其他文件不能访问private func addChildViewController(childVc: UIViewController, title : String, imageName : String) {    // 1.设置子控制器的属性    childVc.title = title    childVc.tabBarItem.image = UIImage(named: imageName)    childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")    // 2.包装导航栏控制器    let childNav = UINavigationController(rootViewController: childVc)    // 3.添加控制器    addChildViewController(childNav)}}

MainViewController.swift 另一种方法 使用json格式的文件 MainViewController.swift

这样的好处是可以在服务器动态生成Tabbar的样式 我现在用的是本地的json文件 主要是要有这样的思想json格式的文件里面的样式:[{"vcName": "HomeViewController","title": "首页","imageName": "tabbar_home"},{ "vcName": "MessageViewController", "title": "消息", "imageName": "tabbar_message_center"},{ "vcName": "DiscoverViewController", "title": "广场", "imageName": "tabbar_discover"},{ "vcName": "ProfileViewController", "title": "我", "imageName": "tabbar_profile"}] //MainViewController.swift 代码 import UIKitclass MainViewController: UITabBarController {override func viewDidLoad() {    super.viewDidLoad()    // 1.获取json文件路径    guard let jsonPath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil) else {        print("没有获取到对应的文件路径")        return    }    // 2.读取json文件中的内容    guard let jsonData = NSData(contentsOfFile: jsonPath) else {        print("没有获取到json文件中数据")        return    }    // 3.将Data转成数组    // 如果在调用系统某一个方法时,该方法最后有一个throws.说明该方法会抛出异常.如果一个方法会抛出异常,那么需要对该异常进行处理    /*     在swift中提供三种处理异常的方式        方式一:try方式 程序员手动捕捉异常            do {                try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)            } catch {                // error异常的对象                print(error)            }        方式二:try?方式(常用方式) 系统帮助我们处理异常,如果该方法出现了异常,则该方法返回nil.如果没有异常,则返回对应的对象            guard let anyObject = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) else {                return            }        方式三:try!方法(不建议,非常危险) 直接告诉系统,该方法没有异常.注意:如果该方法出现了异常,那么程序会报错(崩溃)            let anyObject = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)     */    guard let anyObject = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) else {        return    }    guard let dictArray = anyObject as? [[String : AnyObject]] else {        return    }    // 4.遍历字典,获取对应的信息    for dict in dictArray {        // 4.1.获取控制器的对应的字符串        guard let vcName = dict["vcName"] as? String else {            continue        }        // 4.2.获取控制器显示的title        guard let title = dict["title"] as? String else {            continue        }        // 4.3.获取控制器显示的图标名称        guard let imageName = dict["imageName"] as? String else {            continue        }        // 4.4.添加子控制器        addChildViewController(vcName, title: title, imageName: imageName)    }}private func addChildViewController(childVcName: String, title : String, imageName : String) {    // 0.获取命名空间    guard let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {        print("没有获取命名空间")        return    }    // 1.根据字符串获取对应的Class    guard let ChildVcClass = NSClassFromString(nameSpace + "." + childVcName) else {        print("没有获取到字符串对应的Class")        return    }    // 2.将对应的AnyObject转成控制器的类型    guard let childVcType = ChildVcClass as? UIViewController.Type else {        print("没有获取对应控制器的类型")        return    }    // 3.创建对应的控制器对象    let childVc = childVcType.init()    // 4.设置子控制器的属性    childVc.title = title    childVc.tabBarItem.image = UIImage(named: imageName)    childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")    // 5.包装导航栏控制器    let childNav = UINavigationController(rootViewController: childVc)    // 6.添加控制器    addChildViewController(childNav)}

}

0 0