Skip to content

Instantly share code, notes, and snippets.

@codelynx
Last active October 30, 2020 04:35
Show Gist options
  • Save codelynx/11f1fc1dc8a0de679c05dc88016a2480 to your computer and use it in GitHub Desktop.
Save codelynx/11f1fc1dc8a0de679c05dc88016a2480 to your computer and use it in GitHub Desktop.
Swift Playground: This code demonstrate deepest view controller to a certain view controller conforms to a certain protocol by taking advantage of responder chain.
// The MIT License (MIT)
//
// Copyright (c) 2020 Electricwoods LLC, Kaz Yoshikawa.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// This code demonstrates how to take advantage of using UIResponder chain to reach a certain responder
// skipping over multiple view controllers. The deepest QuaternaryViewController trys to
// reach PrimaryViewController's save() method using UIResponder chain.
import UIKit
import PlaygroundSupport
extension UIResponder {
func findResponder<T>() -> T? {
var responder = self.next
repeat {
if let responder = responder as? T {
return responder
}
responder = responder!.next
} while responder != nil
return nil
}
}
protocol SaveProtocol: AnyObject {
func save()
}
class PrimaryViewController : UIViewController, SaveProtocol {
let secondaryViewController = SecondaryViewController()
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.secondaryViewController.view)
self.addChild(self.secondaryViewController)
self.secondaryViewController.didMove(toParent: self)
}
func save() {
print("Saved !!")
}
}
class SecondaryViewController: UIViewController {
let tertiaryViewController = TertiaryViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.tertiaryViewController.view)
self.addChild(self.tertiaryViewController)
self.tertiaryViewController.didMove(toParent: self)
}
}
class TertiaryViewController: UIViewController {
let quaternaryViewController = QuaternaryViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.quaternaryViewController.view)
self.addChild(self.quaternaryViewController)
self.quaternaryViewController.didMove(toParent: self)
}
}
class QuaternaryViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.save()
}
func save() {
if let saver = self.findResponder() as SaveProtocol? {
saver.save()
}
}
}
PlaygroundPage.current.liveView = PrimaryViewController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment