美文网首页
视图控制器

视图控制器

作者: 草莓Gogirl | 来源:发表于2016-12-02 14:49 被阅读0次
    1. 视图控制器方法
      • – loadView:不要主动调用该方法,当访问控制器view的时候,就会调用该方法。如果要自己创建view,则需要重写该方法。
      • – viewDidLoad:当加载控制器的视图到内存时,该方法被调用。
      • – didReceiveMemoryWarning:接受内存警告消息

    2. 视图控制器生命周期(生命周期的方法)
      • – viewWillAppear:
      • – viewDidAppear:
      • – viewWillDisappear:
      • – viewDidDisappear:

    3. 模态推出(模态推出一定要dismiss)

    4. view是懒加载的

    5. 属性传值

    ViewController
    import UIKit
    
    class ViewController: UIViewController {
        
        //初始化 第一个走的方法
    //    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    //        //1.加载的xib文件 2.bundle(main)
    //        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    //    }
    //    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            //fatalError("init(coder:) has not been implemented")
        }
        
        //加载view 只会走一次
            override func loadView() {
                super.loadView()
    //            //替换当前控制器的view
    //            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    //            self.view = imageView
            }
        
        
       //加载相关资源
        override func viewDidLoad() {
            super.viewDidLoad()
            //视图控制器 //UIControl子类  //手势
           let btn = UIButton(type: .system)
            btn.frame = CGRect(x: 100, y: 100, width: 50, height: 40)
            btn.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
            btn.setTitle("登录", for: .normal)
            self.view.addSubview(btn)
        }
        
        
        
        func btnAction(btn:UIButton){
            //模态推出下一个界面,一般用于注册
            let vc = SecondViewController()
            //1.要推出的下一个控制器
            //2.是否用动画
            //3.推出完成后回调  闭包表达式
            self.present(vc, animated: true){
                
            }
        }
        
        
        
        //视图将要显示在屏幕上
        //viewWillAppear会走很多次  回调时也可用
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
        }
        
        //显示在屏幕上
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
        }
        
        //视图将要从屏幕上消失
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
        }
        
        //视图已经消失
        override func viewDidDisappear(_ animated: Bool) {
            super.viewDidDisappear(animated)
        }
        
        //控制器被销毁
        deinit{
            
        }
        
        
        
        
    
        
        
         //接收到内存警告信息
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    
    SecondViewController
    import UIKit
    
    class SecondViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.cyan
            
            let btn = UIButton(type: .system)
            btn.frame = CGRect(x: 100, y: 100, width: 50, height: 40)
            btn.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
            btn.setTitle("登录", for: .normal)
            self.view.addSubview(btn)
        }
        
        func btnAction(btn:UIButton){
            //当前页面回收回去
            self.dismiss(animated: true, completion: nil)
        
        
        }
    
        
        
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    

    相关文章

      网友评论

          本文标题:视图控制器

          本文链接:https://www.haomeiwen.com/subject/gkobpttx.html