ライフサイクル、およびモデルの導入

1742 ワード

ライフサイクル


モード押し出し

  • ViewControllerでbuttonを作成し、ログインし、buttonをクリックすると次のコントローラが押し出され、2番目のコントローラのbuttonをクリックすると、ViewController
  • に戻る
  • まずViewController中、
  • // ,
    override func loadView() {
            super.loadView()
    // button
    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)
            
    // button 
    func btnAction(btn:UIButton){
                //    
                let vc = SecondViewController()
                //1,   2,   3, 
                self.present(vc,animated: true)
    //          
    // 
        override func viewWillAppear(_ animated: Bool) {
            super.didReceiveMemoryWarning()
            //Dispose of any resources that can be
        }
        // 
        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 {
        }
    
  • そして2番目のコントローラ、
  • // 
    self.view.backgroundColor = UIColor.red
    // button, button 
            let btn = UIButton(type:.system)
            btn.frame = CGRect(x: 100, y: 100, width: 100, height: 40)
            btn.addTarget(self, action: #selector (btnAction(btn:)), for:.touchUpInside)
            btn.setTitle(" ", for: .normal)
            self.view.addSubview(btn)
    // button 
    func btnAction(btn:UIButton) {
            // 
            self.dismiss(animated: true, completion: nil)
        }