视图控制器

weekend 2

视图控制器

  1. 为什么要有视图控制器?
  2. 视图控制器的生命周期
  3. 生命周期的每个方法是干什么的(一些实际的应用场景)
  4. 模态推出
    模态推出之后一定要dismiss
  5. view是懒加载的
  6. 属性传值
import UIKit

class ViewController: UIViewController {
//    override init(nibName nibNameOrNil:String?,bundle nibBundleOrNil:Bundle?){
//        //加载的xib文件
//        //bundle
//        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
//        
//    }
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(corder:) has not been implemented")
//    }
//加载view(只会走一次)
    override func loadView() {
        super.loadView()
        //替换当前控制器的view
//        let imageV = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
//        
//        self.view = imageV
    }
    //加载相关资源
    override func viewDidLoad() {
        super.viewDidLoad()
     
    //视图控制器
    //UIControl类
        //手势
 
    let btn = UIButton(type: .system)
        btn.frame = CGRect(x: 100, y: 100, width: 40, height: 30)
        btn.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        btn.setTitle("登录", for: .normal)
        self.view.addSubview(btn)
    
           }
    func btnAction(btn:UIButton){
        //模态推出下一个界面,一般用于注册
        let vc = SecondViewController()
        //要推出的下一个控制器 
        //是否有动画 
        //推出完成之后回掉
        
        self.present(vc, animated: true){
            
        }
    }
//视图将要显示在屏幕上
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
//视图将要消失在屏幕上
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    //视图已经消失
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }
    //控制器被销毁
    deinit {
        
    }
    
    
    //显示在屏幕上
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
    //接受内存警告
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
        
        
        
    }


}

你可能感兴趣的:(视图控制器)