I'm having trouble comprehending how the view property of UIViewController
calls the viewDidLoad()
method. It doesn't make sense to but I'd like to understand what's happening under the hood. I'm sure this is well explained in the Swift programming guide or maybe even in Apple's reference guide for UIViewController
but right now is too verbose to quite understand. If it is explained in the Swift programming guide, I'm not sure of the correct term to research it further or how this process works. Maybe computed property? However from what I've learned about computed properties is that a computed property does some kind of logic in order to set its variable to a new value or maybe even the initial value. What's troubling me is understanding the concept of how a property calls a function in it's class? Most specifically the view property in UIViewController
that calls the viewDidLoad
method.
Here is my code that helped me stumble across this:
func test_OnViewDidLoad_tableViewIsSet(){
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ItemListViewController")
let sut = viewController as! ItemListViewController
_ = sut.view
XCTAssertNotNil(sut.tableView)
}
Here is my subclassed UIViewController:
import UIKit
class ItemListViewController: UIViewController {
var tableView: UITableView?
override func viewDidLoad() {
tableView = UITableView()
}
}
答案 0 :(得分:1)
这里概述了可能发生的事情(我们没有UIViewController
的源代码(用Objective-C编写)。
class UIViewController: UIResponder {
private var _view: UIView!
var view: UIView! {
get {
if _view == nil {
loadView()
if _view != nil {
viewDidLoad()
}
}
return _view
}
set {
_view = newValue
}
}
}
我确定还有更多内容,但这可以让您大致了解loadView
和viewDidLoad
如何通过访问view
属性来最终被调用。< / p>