2015年1月15日木曜日

swift コードでUITabBarController

コードでUITabBarControllerを実装する方法のメモ

環境

  • Xcode6.1

objective-cと基本的なやり方は変わりません。tabごとのviewを作成して、UITabBarControllerを継承したTabBarControllerに設定するだけです。


class MainTabBarController: UITabBarController {
    
    var homeViewController: HomeViewController!
    var infoViewController: InfoViewController!
    var itemViewController: ItemViewController!
    var otherViewController: OtherViewController!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        homeViewController = HomeViewController()
        infoViewController = InfoViewController()
        magazineViewController = MagazineViewController()
        otherViewController = OtherViewController()
        
        //表示するtabItemを指定
        homeViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Featured, tag: 1)
        infoViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 2)
        magazineViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 3)
        otherViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Featured, tag: 4)
        
        // タブで表示するViewControllerを配列に格納します。
        let myTabs = NSArray(objects: homeViewController!, infoViewController!, magazineViewController!, otherViewController!)
        
        // 配列をTabにセットします。
        self.setViewControllers(myTabs, animated: false)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}


以上。つーか、objective-Cと同じですね。

参考

この記事がお役にたちましたらシェアをお願いします

このエントリーをはてなブックマークに追加

0 件のコメント:

コメントを投稿

Related Posts Plugin for WordPress, Blogger...