Swift3.0-学习之路之导航控制器

根控制器

//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, with event: UIEvent?) {
        self.navigationController?.pushViewController(PushViewController(), animated: true)
            // 跳转
//        self.navigationController?.present(PushViewController(), animated: true, completion: { 
//            
//        })
    }
}

效果图:


Swift3.0-学习之路之导航控制器_第1张图片
控制器

视图控制器的生命周期

//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

你可能感兴趣的:(Swift3.0-学习之路之导航控制器)