Skip to content

Instantly share code, notes, and snippets.

@cipolleschi
Created May 19, 2021 15:48
Show Gist options
  • Save cipolleschi/191bd1c08aeea40e4d2c96e0df110501 to your computer and use it in GitHub Desktop.
Save cipolleschi/191bd1c08aeea40e4d2c96e0df110501 to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
class MyViewControllerA : UIViewController {
let viewControllerB = MyViewControllerB()
override func loadView() {
print("MyViewControllerA - loadView Invoked")
let view = MyViewA()
view.push = { [unowned self] in
self.present(self.viewControllerB, animated: true, completion: nil)
}
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
print("MyViewControllerA - viewDidLoad Invoked")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("MyViewControllerA - viewWillAppear Invoked")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("MyViewControllerA - viewDidAppear Invoked")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
print("MyViewControllerA - viewWillDisappear Invoked")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("MyViewControllerA - viewDidDisappear Invoked")
}
}
class MyViewControllerB : UIViewController {
override func loadView() {
print("MyViewControllerB - loadView Invoked")
let view = MyViewB()
view.pop = { [unowned self] in
self.dismiss(animated: true, completion: nil)
}
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
print("MyViewControllerB - viewDidLoad Invoked")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("MyViewControllerB - viewWillAppear Invoked")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("MyViewControllerB - viewDidAppear Invoked")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillAppear(animated)
print("MyViewControllerB - viewWillDisappear Invoked")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("MyViewControllerB - viewDidDisappear Invoked")
}
}
class MyViewA: UIView {
typealias Interaction = () -> ()
var push: Interaction?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
self.addSubview(label)
let button = UIButton()
button.setTitle("Push", for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
button.frame = CGRect(x: 150, y: 275, width: 100, height: 20)
self.addSubview(button)
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}
@objc func buttonTapped(_ sender: UIControl) {
self.push?()
}
required init?(coder: NSCoder) {
fatalError("We don't need this")
}
}
class MyViewB: UIView {
typealias Interaction = () -> ()
var pop: Interaction?
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white
let button = UIButton()
button.setTitle("Close", for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
button.frame = CGRect(x: 150, y: 275, width: 100, height: 20)
self.addSubview(button)
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}
@objc func buttonTapped(_ sender: UIControl) {
self.pop?()
}
required init?(coder: NSCoder) {
fatalError("We don't need this")
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewControllerA()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment