swift代码之路(一)

从搭建项目开始说起 在AppDelegate.swift 中application中这样写

class AppDelegate:UIResponder, UIApplicationDelegate {


    var window:UIWindow?



    func application(application:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {

        // Override point for customization after application launch.

        // 创建窗口

        window =UIWindow(frame: UIScreen.mainScreen().bounds)

        window?.backgroundColor =UIColor.whiteColor()

        window?.rootViewController =SPFMainViewController()

        window?.makeKeyAndVisible()

        returntrue

    }


}

在继承自UITabBarController控制器中这样写

需要建立自己页面的控制器

import UIKit

/// 主控制器

class SPFMainViewController: UITabBarController {


    overridefunc viewDidLoad() {

        super.viewDidLoad()

        setupChildControllers()

        // Do any additional setup after loading the view.

    }


    overridefunc didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }


}

//extension类似于OC中的分类,在Swift中还可以切分代码块

extension SPFMainViewController {

    /**

     *  设置所有子控制器

     */

    privatefunc setupChildControllers() {

        addChildViewController(SPFFirstViewController(), title:"首页", imageName:"tabbar_first")

        addChildViewController(SPFSpecialViewController(), title:"专题", imageName:"tabbar_special")

        addChildViewController(SPFClassifyViewController(), title:"分类", imageName:"tabbar_class")

        addChildViewController(SPFShopCarViewController(), title:"购物车", imageName:"tabbar_shopcar")

         addChildViewController(SPFProfileViewController(), title:"我的", imageName:"tabbar_me")

    }

    /**

     使用字典创建一个子控制器

     

     - parameter dict: 信息字典 [clsNme ,title, imageName]

     

     - returns: 子控制器

     */

    privatefunc addChildViewController(controller:UIViewController, title:String, imageName:String){

        

        controller.tabBarItem.image =UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)

        controller.tabBarItem.selectedImage =UIImage(named: imageName +"_selected")?.imageWithRenderingMode(.AlwaysOriginal)

        

       

        controller.tabBarItem.title = title

        controller.title = title

        //设置tabbar的标题字体颜色(大小)

        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)

//设置字体颜色

        //controller.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)

        let nav =SPFNavigationController()

        nav.addChildViewController(controller)

        addChildViewController(nav)

    }

}



注释都标的很详细,这是swift2.0的写法   3.0在设置按钮norml的地方与此处稍有不同大家可以上网搜索语法变化哟

你可能感兴趣的:(Swift)