Skip to content

Instantly share code, notes, and snippets.

View chriswebb09's full-sized avatar

Christopher Webb chriswebb09

View GitHub Profile
class ViewTraverser {
private func traverseSuperViews(view: UIView) -> [Int : UIView] {
var views: [Int: UIView] = [:]
var inputView: UIView? = view
while inputView != nil {
guard let tag = inputView?.tag, let view = inputView else { continue }
views[tag] = view
inputView = view.superview
}
return views
class ViewTraverser {
// Traverse superviews
private func checkForSuper(view: UIView?, views: [Int: UIView]) -> UIView? {
guard let view = view else { return nil }
if views[view.tag] != nil {
return view
}
import UIKit
class ViewController: UIViewController {
let viewOne = UIView()
let viewTwo = UIView()
let viewThree = UIView()
let intermediaryView = UIView()
let viewSuper = UIView()
let traverser = ViewTraverser()
import UIKit
class ViewController: UIViewController {
// Properties
override func viewDidLoad() {
// setup
let nameView = traverser.commonSuper(viewOne: viewOne, viewTwo: viewTwo)
print(nameView?.tag)
}
class MainViewController: UIViewController {
@IBOutlet var mainView: MainView!
override func viewDidLoad() {
super.viewDidLoad()
mainView.myButton.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
}
func buttonTap() {
class MainView: UIView {
@IBOutlet weak var myTextfield: UITextField!
@IBOutlet weak var myButton: UIButton!
}
protocol MainViewDelegate: class {
func searchButtonTappedWithTerm(with searchTerm: String)
}
class MainView: UIView {
@IBOutlet private weak var myTextfield: UITextField!
@IBOutlet private weak var myButton: UIButton!
weak var delegate: MainViewDelegate?
override func layoutSubviews() {
super.layoutSubviews()
myButton.addTarget(self, action: #selector(buttonTap), for: .touchUpInside)
extension ViewController: MainViewDelegate {
func searchButtonTappedWithTerm(with searchTerm: String) {
print("The text in UITextField is: \(searchTerm)")
}
}
class MainViewController: UIViewController {
@IBOutlet var mainView: MainView!
override func viewDidLoad() {
super.viewDidLoad()
mainView.delegate = self
}
}