多页面
根据需要建立以.swift档案,并各自命名
如:IntroViewController、ArticleViewController及ArticleDetailViewController
在viewDidLoad() 中定义底色、页面Label、以及UIButton
定义底色
self.view.backgroundColor = UIColor.white
定义页面标题
` let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: fullSize.width, height: 40))
myLabel.center = CGPoint(x: fullSize.width * 0.5, y: fullSize.height * 0.08)
myLabel.textAlignment = .center
myLabel.text = "首頁"
self.view.addSubview(myLabel)`
定义需要跳转页面的UIButton
定义 var变量 myButton
` var myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))`
设置标题、背景颜色、位置
myButton.setTitle("Article", for: .normal)
myButton.backgroundColor = UIColor.lightGray
myButton.center = CGPoint(x: fullSize.width * 0.5, y: fullSize.height * 0.2)
操作动作关联,建立前往 Article 页面的UIButton
myButton.addTarget(nil, action: #selector(ViewController.goArticle), for: .touchUpInside)
Quick Help中addTarget的用法
以及
self.view.addSubview(myButton)
在ViewController中加上按下按钮执行动作的方法:
@objc func goArticle() {
let articleVC = ArticleViewController()
self.present(articleVC, animated: true, completion: nil)
}
Quick Help中present的用法
页面开启退出的过程
在页面开启及退出的各个阶段,都可以加入自定义的程式
在首页ViewController中定义
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
mylabel.text = "hello"
print("viewWillAppear")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("viewDidAppear")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("viewWillDisappear")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("viewDidDisappear")
}