ICoder
ICoder

Reputation: 1347

How to set touch events in subviews?

i have a main view controller with two subview in it,one is note-view and other one is share-view . When i click the cell of the tableview that is in the main-view controller ,it popups note-view,there is button in the note-view ,if the user tap the button it pop sup a another subview ,the share-view .But i need to close the share view when the user tap the note-view.Is that possible to implement a touch event in a subview ? I have done this coding but result is error in viewdidload

NotesView *label =[[NotesView alloc]init];
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(NoteTap)]     autorelease];
[label addGestureRecognizer:tapGesture];
[tapGesture release];

then

-(void)NoteTap
{
    UIView *langview = (UIView *)[self.view viewWithTag:120]; 
    //ViewWithTag Number should be same as used while allocating
    [langview removeFromSuperview];
}

Upvotes: 0

Views: 298

Answers (1)

Mahir
Mahir

Reputation: 1684

Inside touchesBegan: of the view controller, you can check to see if a touch event takes places within the frame of the subview like so.

CGPoint touchPoint = [touch locationInView:self];

if (CGRectContainsPoint(label.frame, touchPoint)) {

 UIView *langview = (UIView *)[self.view viewWithTag:120]; 
 [langview removeFromSuperview];

}

Upvotes: 2

Related Questions