Junior — Middle
Can you explain what happens in the viewDidLoad() method in iOS development?
sobes.tech AI
Answer from AI
The viewDidLoad() method is called once in the view controller's (UIViewController) lifecycle after its view has been loaded into memory but before it appears on the screen. In this method, initial interface setup, data initialization, adding subviews, delegate configuration, and other one-time setup tasks are typically performed.
Example:
override func viewDidLoad() {
super.viewDidLoad()
// Set background color
view.backgroundColor = .white
// Load data
loadData()
// Add a button
let button = UIButton(type: .system)
button.setTitle("Press me", for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
view.addSubview(button)
button.center = view.center
}