Swift tabbarController

自定义实现 swift版本的tabbarController,很常用,容易扩展和维护 ,更多好的开源请关注我的github地址

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


var navigationVC : NSMutableArray!

func controllerWithBarTitle (title : String , viewControllerClass :UIViewController , tag: NSInteger , tabbarIconImageName :String )
{
    let viewController : UIViewController =   viewControllerClass;

    let navigationVC = UINavigationController(rootViewController: viewController);

    let tabbarAPengDaiImage = UIImage(named: tabbarIconImageName);

    navigationVC.tabBarItem = UITabBarItem(title:title,image:tabbarAPengDaiImage,tag:tag);
    if((self.navigationVC) == nil)
    {
        self.navigationVC = NSMutableArray()
    }
    self.navigationVC! .addObject(navigationVC);

}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //  UITabBarController
    let tabBarController = UITabBarController();

    self .controllerWithBarTitle("AAA", viewControllerClass: AAAViewController(), tag: 1, tabbarIconImageName: "TabBar_HomeNormal.png");

    self .controllerWithBarTitle("BB财", viewControllerClass: InvestmentViewController(), tag: 2, tabbarIconImageName: "TabBar_InvestmentNormal.png");

    self .controllerWithBarTitle("CC户", viewControllerClass: AccountViewController(), tag: 3, tabbarIconImageName: "TabBar_AccountNormal.png");

    self .controllerWithBarTitle("邀请好友", viewControllerClass: InviteViewController(), tag: 4, tabbarIconImageName: "TabBar_InviteNormal.png");

    print(self.navigationVC);


    tabBarController.viewControllers = self.navigationVC as?[UIViewController];
    /*
    as操作符用来把某个实例转型为另外的类型,由于实例转型可能失败,因此Swift为as操作符提供了两种形式:选项形式as?和强制形式as

    选项形式(as?)的操作执行转换并返回期望类型的一个选项值,如果转换成功则返回的选项包含有效值,否则选项值为 nil 强制形式(as )的操作执行一个实例到目的类型的强制转换,因此使用该形式可能触发一个运行时错误。
    */

    self.window!.rootViewController = tabBarController;
    /*
    self.window!.和self.window?.区别
    个人理解
    ! 是强制取值( forced- value) 表达式,
    ? 是(optional chaining expression) 可选链表达式。
    ?是你不确定opt里面有没有东西,!是你确定里面有东西,当然没有东西的也可以!,但是会分分钟崩溃给你看 ! 号编译器可以分析你的代码

    */


    return true
}