Joe Blow Joe Blow - 1 year ago 61
Swift Question

Swift, for some UIViews to their overall controller when clicked

I have a


There are a dozen UIView in the scene,

(Some are direct subviews, some are further down.)

There are even container views in the chain between the highest controller and the Dot items.

The dozen Dot items know if they are tapped...

class Dot:UIView
private var tap:UITapGestureRecognizer? = nil
override func awakeFromNib()
tap = UITapGestureRecognizer(target:self,
action: #selector(SnapDot.handleTap(_:)))
func handleTap(g:UITapGestureRecognizer)
print("user tapped on this particular Dot...")

I want
to know when one of the
is tapped.

class DotBoss:UIViewController
func oneDotWasClicked(d:Dot)

How to do this perfectly in Swift?

Answer Source

A Dot (UIView) and its ultimately controlling DotBoss (UIViewController) are both responders, and are links along the responder chain. Therefore, a Dot can call a method in its DotBoss by walking the responder chain until it comes to the DotBoss:

func handleTap(g:UITapGestureRecognizer) {
    // ... other stuff can go here if necessary ...
    var r : UIResponder = self
    repeat { r = r.nextResponder()! } while !(r is DotBoss)
    (r as! DotBoss).oneDotWasClicked(self)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download