Skip to content

Instantly share code, notes, and snippets.

@eldaroid
Last active October 7, 2024 19:02
Show Gist options
  • Save eldaroid/430f11ed2dac108bcb2d5d35e31e3bc2 to your computer and use it in GitHub Desktop.
Save eldaroid/430f11ed2dac108bcb2d5d35e31e3bc2 to your computer and use it in GitHub Desktop.
RU: Исследуем жизненный цикл ViewController. EN: Let's study the ViewController life cycle
final class ViewController: UIViewController {
private let name = "🖥️"
private let infoLabel: UILabel = {
let label = UILabel()
label.text = "AppDelegate Life Cycle"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let switchButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Switch to Second VC", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
init() {
super.init(nibName: nil, bundle: nil)
Logger.shared.log("\(name) - 1) \(#function)")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
Logger.shared.log("\(name) - 2) \(#function)")
view = UIView()
}
override func viewDidLoad() {
super.viewDidLoad()
Logger.shared.log("\(name) - 3) \(#function)")
view.backgroundColor = .blue
view.addSubview(infoLabel)
view.addSubview(switchButton)
// Настройка автолэйаута для infoLabel и switchButton
NSLayoutConstraint.activate([
infoLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
infoLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
switchButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
switchButton.topAnchor.constraint(equalTo: infoLabel.bottomAnchor, constant: 20)
])
switchButton.addTarget(self, action: #selector(switchToSecondVC), for: .touchUpInside)
}
@objc private func switchToSecondVC() {
let secondVC = SecondViewController()
navigationController?.pushViewController(secondVC, animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Logger.shared.log("\(name) - 4) \(#function)")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
Logger.shared.log("\(name) - 5) \(#function)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
Logger.shared.log("\(name) - 6) \(#function)")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Logger.shared.log("\(name) - 7) \(#function)")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Logger.shared.log("\(name) - 8) \(#function)")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
Logger.shared.log("\(name) - 9) \(#function)")
}
deinit {
Logger.shared.log("\(name) - 10) \(#function)")
}
}
final class SecondViewController: UIViewController {
private let name = "🖥️🖥️"
init() {
super.init(nibName: nil, bundle: nil)
Logger.shared.log("\(name) - 1) \(#function)")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
Logger.shared.log("\(name) - 2) \(#function)")
view = UIView()
view.backgroundColor = .green
}
override func viewDidLoad() {
super.viewDidLoad()
Logger.shared.log("\(name) - 3) \(#function)")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Logger.shared.log("\(name) - 4) \(#function)")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
Logger.shared.log("\(name) - 5) \(#function)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
Logger.shared.log("\(name) - 6) \(#function)")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Logger.shared.log("\(name) - 7) \(#function)")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Logger.shared.log("\(name) - 8) \(#function)")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
Logger.shared.log("\(name) - 9) \(#function)")
}
deinit {
Logger.shared.log("\(name) - 10) \(#function)")
}
}
@eldaroid
Copy link
Author

default.mp4

@eldaroid
Copy link
Author

eldaroid commented Jun 24, 2024

UIViewController

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment