Swift3.0-學習之路之導航控制器

weixin_33860722發表於2017-04-22

根控制器

//1.建立UIWindow物件
self.window = UIWindow.init(frame: UIScreen.main.bounds)
//2.設定根檢視控制器
let rootViewController = MainViewController()
self.window?.rootViewController = rootViewController
//3.設定背景顏色
self.window?.backgroundColor = UIColor.red
self.window?.makeKeyAndVisible()

建立一個main控制器,繼承自UITabBarController

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let home = HomeViewController()
        home.title = "首頁"
        let homeVC = UINavigationController.init(rootViewController: home)
        
        
        let shop = ShopCatViewController()
        shop.title = "購物車"
        let shopVC = UINavigationController.init(rootViewController: shop)
        shop.tabBarItem.badgeValue = "99"
        
        let mine = MineViewController()
        mine.title = "個人中心"
        let mineVC = UINavigationController.init(rootViewController: mine)
        
        self.viewControllers = [homeVC, shopVC, mineVC]
    }
}

首頁設定

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "首頁"
        //設定導航欄背景顏色
        self.navigationController?.navigationBar.barTintColor = UIColor.green
        self.view.backgroundColor = UIColor.red
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.navigationController?.pushViewController(PushViewController(), animated: true)
            // 跳轉
//        self.navigationController?.present(PushViewController(), animated: true, completion: { 
//            
//        })
    }
}

效果圖:


5684426-a34fdfb856db509f.png
控制器

檢視控制器的生命週期

//MARK: - 生命週期
    //注意:重寫生命週期相關的方法的時候必須通過super去呼叫父類的相關方法
    //1.開始建立檢視控制器的view的屬性的時候會自動呼叫(建立一個檢視控制器只會呼叫一次)
    override func loadView() {
        super.loadView()
        print("建立view")
    }
    
    //2.每次view將要出現的時候會呼叫(可能會被呼叫多次)
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("view將要出現")
    }
    
    //3.每次view已經出現的時候會呼叫(可能會被呼叫多次)
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("view已經出現")
    }
    
    //4.view屬性已經載入完成之後會自動呼叫(建立一個檢視控制器只會呼叫一次)
    //一般在這個方法中去搭建當前檢視控制器對應的介面
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        print("view載入完成")
    }
    
    //5.每次view將要消失的時候會呼叫(可能會被呼叫多次)
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        print("view將要消失")
    }
    
    //6.每次view已經消失的時候會呼叫(可能會被呼叫多次)
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        print("view已經消失")
    }
    //接收到記憶體警告的時候自動呼叫
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

到這裡,建立控制器的簡單使用就介紹完了。
程式碼:https://github.com/Baiyongyu/Swift-RootController.git

相關文章