Skip to content

Instantly share code, notes, and snippets.

@lucaswkuipers
Last active June 21, 2022 04:30
Show Gist options
  • Save lucaswkuipers/3f4eaa5cb416e3da7d94cb314dafd136 to your computer and use it in GitHub Desktop.
Save lucaswkuipers/3f4eaa5cb416e3da7d94cb314dafd136 to your computer and use it in GitHub Desktop.
import UIKit
protocol ReusableViewControllerDelegate {
func loadView()
func viewDidLoad()
func viewWillAppear(_ animated: Bool)
func viewWillLayoutSubviews()
func viewDidLayoutSubviews()
func viewDidAppear(_ animated: Bool)
func viewWillDisappear(_ animated: Bool)
func viewDidDisappear(_ animated: Bool)
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
func traitCollectionDidChange(from previousTraitCollection: UITraitCollection?, to currentTraitCollection: UITraitCollection?)
func didReceiveMemoryWarning()
}
extension ReusableViewControllerDelegate {
func loadView() {}
func viewDidLoad() {}
func viewWillAppear(_ animated: Bool) {}
func viewWillLayoutSubviews() {}
func viewDidLayoutSubviews() {}
func viewDidAppear(_ animated: Bool) {}
func viewWillDisappear(_ animated: Bool) {}
func viewDidDisappear(_ animated: Bool) {}
func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {}
func traitCollectionDidChange(from previousTraitCollection: UITraitCollection?, to currentTraitCollection: UITraitCollection?) {}
func didReceiveMemoryWarning() {}
}
final class ReusableViewController: UIViewController {
var delegate: ReusableViewControllerDelegate?
private let contentView: UIView
init(with view: UIView) {
self.contentView = view
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
super.loadView()
self.view = contentView
delegate?.loadView()
}
override func viewDidLoad() {
super.viewDidLoad()
delegate?.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
delegate?.viewWillAppear(animated)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
delegate?.viewWillLayoutSubviews()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
delegate?.viewDidLayoutSubviews()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
delegate?.viewDidAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
delegate?.viewWillDisappear(animated)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
delegate?.viewDidDisappear(animated)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
delegate?.viewWillTransition(to: size, with: coordinator)
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
delegate?.traitCollectionDidChange(from: previousTraitCollection, to: traitCollection)
}
override func didReceiveMemoryWarning() {
delegate?.didReceiveMemoryWarning()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment