有没有人知道一个很好的教程,深入解释了视图控制器的生命周期.我已经阅读了文档,所以请不要将我与他们联系起来.我只是在寻找每个函数的实际解释,例如viewDidLoad和viewWillAppear,viewWillLayoutSubviews等,以及何时最好将它们与Swift中的示例一起使用.如果没有教程,任何人都愿意在答案中解释它们.
小智 7
我使用swift为你显示代码.
import UIKit
class LifeCycleViewController: UIViewController {
// MARK: -property
lazy var testBtn: UIButton! = {
var btn: UIButton = UIButton()
btn.backgroundColor = UIColor.redColor()
return btn
}()
// MARK: -life cycle
override func viewDidLoad() {
super.viewDidLoad()
println("View has loaded")
// set the superView backgroudColor
self.view.backgroundColor = UIColor.blueColor()
// add testBtn to the superView
self.view.addSubview(self.testBtn)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("View will appear")
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
println("View has appeared")
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
println("View will disappear")
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
println("View has desappeared")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
println("SubViews will layout")
// layout subViews
self.testBtn.frame = CGRectMake(100, 100, 100, 100)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
println("SubViews has layouted")
var testBtn_Width = self.testBtn.frame.width
println("testBtn's width is \(testBtn_Width)")
}
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
View has loaded
View will appear
SubViews will layout
SubViews has layouted
testBtn's width is 100.0
SubViews will layout
SubViews has layouted
testBtn's width is 100.0
View has appeared
Run Code Online (Sandbox Code Playgroud)
您可以清楚地看到ViewController的生命周期.
| 归档时间: |
|
| 查看次数: |
4060 次 |
| 最近记录: |