24
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでTabBarの仕組みを EmptyApplicationから作ってみた。

Last updated at Posted at 2014-06-07

xibファイルと連携する方法。
Storyboardでの画面遷移管理がまだ苦手なので・・・。
ちなみに言語の仕様や文法を完全には理解していないので、記述方法等が間違っていたらご指摘願います。

AppDelegate.swift

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
                            
    var window: UIWindow?


    func application(application: UIApplication, 
                    didFinishLaunchingWithOptions 
                    launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // Override point for customization after application launch.
        self.window!.backgroundColor = UIColor.whiteColor()
        
        //Create main ViewControllers
        var firstViewController:FirstViewController = 
                FirstViewController(nibName: "FirstViewController", bundle: nil)
        var secondViewController:SecondViewController = 
                SecondViewController(nibName: "SecondViewController", bundle: nil)
        var thirdViewController:ThirdViewController = 
                ThirdViewController(nibName: "ThirdViewController", bundle: nil)
        
        //Create NavigationControllers
        var firstNavigationController:UINavigationController = 
                UINavigationController(rootViewController:firstViewController)
        var secondNavigationController:UINavigationController = 
                UINavigationController(rootViewController:secondViewController)
        var thirdNavigationController:UINavigationController = 
                UINavigationController(rootViewController:thirdViewController)
        var naviControllerList:Array<UINavigationController> = 
                [
                    firstNavigationController, 
                    secondNavigationController, 
                    thirdNavigationController
                ]
        
        //Create TabBarController
        var tabBarController:UITabBarController = UITabBarController()
        tabBarController.setViewControllers(naviControllerList, animated: true)
        
        self.window!.rootViewController = tabBarController
        self.window!.makeKeyAndVisible()
        return true
    }

各UIViewController init の部分が不安であるが…。

FirstViewController.swift
import Foundation
import UIKit

class FirstViewController : UIViewController
{
    init(nibName:String!, bundle: NSBundle!)
    {
        super.init(nibName: nibName, bundle: bundle)
        
        //Create Navigationbar
        self.navigationItem.title = "First VIEW CONTROLLER"
        
        //Create TabBar
        self.title = "First"

    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

}

SecondViewController.swift
import Foundation
import UIKit

class SecondViewController : UIViewController
{
    init(nibName:String!, bundle: NSBundle!)
    {
        super.init(nibName: nibName, bundle: bundle)
            
        //Create Navigationbar
        self.navigationItem.title = "Second VIEW CONTROLLER"
        
        //Create TabBar
        self.title = "Second"

    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
    
}

ThirdViewController.swift
import Foundation
import UIKit

class ThirdViewController :UIViewController
{
    init(nibName:String!, bundle: NSBundle!)
    {
        super.init(nibName: nibName, bundle: bundle)
        
        //Create Navigationbar
        self.navigationItem.title = "Third VIEW CONTROLLER"
        
        //Create TabBar
        self.title = "Third"

    }
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
    
}

この他、AppDelegate.swiftファイルでnibName設定しているxibファイルが必要となります。

24
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?