How to detect touch event in child viewcontroller in ios

Dhaval H. Nena picture Dhaval H. Nena · Jul 16, 2014 · Viewed 7.9k times · Source

I have created a sideview like menu for the iPhone for which I have used container ship concept for adding child view controller in the parent view controller. When user taps on menu button on the navigation bar I just change the frame of child view controller to animate it like a revealing menu so that the child view controller will be clipped a half of its frame to the right.

Now my problem is whenever user taps on any portion of the child view controller I just want to notify its parent view controller that touch event has fired on its child view controller so that parent view controller can reset the frame of child view controller to animate it like close the half revealed menu.

I have used tap gesture and have added it to the every child view of the child view controllers subviews. so tap gesture on any of the subview will notify the parent view controller about touch event.

Touch event works fine for the main view of the child view controller but any touches on any button doesn't recognizing the TAP event.

I don't know where am I mistaking. please help me how to notify the parent view controller about any touches in its child view controller. Thanks in advance.

Answer

You should not do this

"I have used tap gesture and have added it to the every child view of the child view controllers subviews"

You should do add one overlay view on top of the parentviewcontroller's view after you opened the menu. so it will stay on top of left and right views.

you should add tap recogniser on it to destroy/hide the overlay view and do Menu Hide animation there. See the below code

-(void)afterMenuOpened{

    UIViewController *parentViewController = yourParentViewController;

    UIView *aOverLayView = [[UIView alloc]initWithFrame:parentViewController.view.bounds];

    aOverLayView.backgroundColor = [UIColor clearColor];

    UITapGestureRecognizer *tapRecog = [[UITapGestureRecognizer alloc] initWithTarget:parentViewController action:@selector(OverLayDidTap:)];

    tapRecog.numberOfTapsRequired = 1;

    [aOverLayView addGestureRecognizer:tapRecog];

    [parentViewController.view addSubview:aOverLayView];


}

- (void)OverLayDidTap:(UITapGestureRecognizer*)sender {
//    sender.view.hidden = YES;
    NSLog(@"Hide Menu by resetting the menu frame");
}