生命周期
模態(tài)推出
- 在ViewController中創(chuàng)建一個button草雕,登錄,當點擊button時 推出下一個控制器赃梧,然后當點擊第二個控制器中的button時 滤蝠,再回到ViewController
- 首先是ViewController中,
//構(gòu)造一個架子授嘀,
override func loadView() {
super.loadView()
//創(chuàng)建一個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){
//模態(tài)推出下一個界面 一般用于注冊
let vc = SecondViewController()
//1物咳,要推出的下一個控制器 2,是否有動畫 3蹄皱,推出完成之后回調(diào)
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)
}
//視圖已經(jīng)消失
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
}
//控制器被銷毀
deinit {
}
//添加一個背景顏色
self.view.backgroundColor = UIColor.red
//然后創(chuàng)建一個button,這個button將要實現(xiàn)點擊推到第一個控制器
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)
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者