Last active
January 25, 2017 15:24
-
-
Save BrandonShega/41e7f51c8710efe355f0dfed43b7ce5e to your computer and use it in GitHub Desktop.
Delegate Demonstration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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