Swift-学习

生命周期,以及模态推出

2016-12-14  本文已影响0人  文艺小年青

生命周期

模态推出

//构造一个架子,
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 {
    }
//添加一个背景颜色
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)
    }
上一篇下一篇

猜你喜欢

热点阅读