0
votes

Suppose I have a button that everytime I clicked it will generate a random number of subviews. Is there anyway to detect the subview touched?

1
I already know there's a way that I add all the generated view to an array and do traversing through the array to get the touched view. But, I don't know if there is better solution. - Quang L.

1 Answers

0
votes

You can make use of touchesBegan method and use the view property of the UITouch instance passed:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    //use touch.view property to get the subview touched
}

Remember to set the property userInteractionEnabled to YES in each of your subviews.