Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Last active January 25, 2017 15:24
Show Gist options
  • Save BrandonShega/41e7f51c8710efe355f0dfed43b7ce5e to your computer and use it in GitHub Desktop.
Save BrandonShega/41e7f51c8710efe355f0dfed43b7ce5e to your computer and use it in GitHub Desktop.
Delegate Demonstration
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
var str = "Hello, playground"
protocol EditViewControllerDelegate: class {
func didFinishEditing()
}
class EditViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Edit View Controller"
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
delegate?.didFinishEditing()
}
}
class FirstViewController: UIViewController {
lazy var editButton: UIButton = {
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 30))
button.setTitle("Edit", for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(FirstViewController.showEditController), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "First View Controller"
view.backgroundColor = .red
view.addSubview(editButton)
}
func showEditController() {
let evc = EditViewController()
evc.delegate = self
navigationController?.pushViewController(evc, animated: true)
}
}
extension FirstViewController: EditViewControllerDelegate {
func didFinishEditing() {
print("editing complete going back to VC1")
}
}
class SecondViewController: UIViewController {
lazy var editButton: UIButton = {
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 30))
button.setTitle("Edit", for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(FirstViewController.showEditController), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Second View Controller"
view.backgroundColor = .blue
view.addSubview(editButton)
}
func showEditController() {
let evc = EditViewController()
evc.delegate = self
navigationController?.pushViewController(evc, animated: true)
}
}
extension SecondViewController: EditViewControllerDelegate {
func didFinishEditing() {
print("editing complete going back to VC2")
}
}
class MainViewController: UIViewController {
lazy var firstButton: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 100, width: 200, height: 30))
button.setTitle("First Controller", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(MainViewController.pushFirstController), for: .touchUpInside)
return button
}()
lazy var secondButton: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 150, width: 200, height: 30))
button.setTitle("Second Controller", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(MainViewController.pushSecondController), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(firstButton)
view.addSubview(secondButton)
view.backgroundColor = .white
}
func pushFirstController() {
let fvc = FirstViewController()
navigationController?.pushViewController(fvc, animated: true)
}
func pushSecondController() {
let svc = SecondViewController()
navigationController?.pushViewController(svc, animated: true)
}
}
let mvc = MainViewController()
let nvc = UINavigationController(rootViewController: mvc)
PlaygroundPage.current.liveView = nvc
protocol EditViewControllerDelegate: class {
func didFinishEditing()
}
class EditViewController: UIViewController {
weak var delegate: EditViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Edit View Controller"
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
delegate?.didFinishEditing()
}
}
class FirstViewController: UIViewController {
lazy var editButton: UIButton = {
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 30))
button.setTitle("Edit", for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(FirstViewController.showEditController), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "First View Controller"
view.backgroundColor = .red
view.addSubview(editButton)
}
func showEditController() {
let evc = EditViewController()
evc.delegate = self
navigationController?.pushViewController(evc, animated: true)
}
}
extension FirstViewController: EditViewControllerDelegate {
func didFinishEditing() {
print("editing complete going back to VC1")
}
}
class MainViewController: UIViewController {
lazy var firstButton: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 100, width: 200, height: 30))
button.setTitle("First Controller", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(MainViewController.pushFirstController), for: .touchUpInside)
return button
}()
lazy var secondButton: UIButton = {
let button = UIButton(frame: CGRect(x: 0, y: 150, width: 200, height: 30))
button.setTitle("Second Controller", for: .normal)
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(MainViewController.pushSecondController), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(firstButton)
view.addSubview(secondButton)
view.backgroundColor = .white
}
func pushFirstController() {
let fvc = FirstViewController()
navigationController?.pushViewController(fvc, animated: true)
}
func pushSecondController() {
let svc = SecondViewController()
navigationController?.pushViewController(svc, animated: true)
}
}
class SecondViewController: UIViewController {
lazy var editButton: UIButton = {
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 30))
button.setTitle("Edit", for: .normal)
button.backgroundColor = .white
button.setTitleColor(.black, for: .normal)
button.addTarget(self, action: #selector(FirstViewController.showEditController), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Second View Controller"
view.backgroundColor = .blue
view.addSubview(editButton)
}
func showEditController() {
let evc = EditViewController()
evc.delegate = self
navigationController?.pushViewController(evc, animated: true)
}
}
extension SecondViewController: EditViewControllerDelegate {
func didFinishEditing() {
print("editing complete going back to VC2")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment