Kevin
Kevin

Reputation:

Passing a touch event to all subviews of a View Controller

I have a view controller which creates multiple subviews on top of it. All subviews and the view controller accept touches. How can i communicate the touch point information to all the subviews on the screen? Keep in mind that each subview covers the entire screen so after a few additions its a bit like pages in a book. That is, any subviews below the top subview can't be touched directly by the user. I have tried using [self.nextResponder touchesBegan:touches withEvent:event] however this only sends the touch information from the top subview to the superview bypassing all other subviews on the screen. Thanks

Upvotes: 4

Views: 7846

Answers (3)

jackbravo
jackbravo

Reputation: 1369

Imlement your touch method only in the superview. From there iterate over all subviews and send them the touch method.

Upvotes: 0

Biranchi
Biranchi

Reputation: 16327

Alternatively you can send NSNotification to all the subviews. And in the subvews class you can have a better control and can handle the notifications to perform some task.

Upvotes: -2

deanWombourne
deanWombourne

Reputation: 38475

I would put another subview ontop of everything. This would grab all the touches that you were interested in and pass them back to the view controller. Your view controller would then iterate through all it's child UIViews, giving them the events.

i.e. something like

for (UIView *view in subviews)
  [view touchesBegan:touches withEvent:event];

though that's assuming that all the subviews you want to pass events to are children of your view controller.

Upvotes: 3

Related Questions