Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect touch event in child viewcontroller in ios

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.

like image 683
Dhaval H. Nena Avatar asked Jul 16 '14 11:07

Dhaval H. Nena


2 Answers

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");
}
like image 108
Vijay-Apple-Dev.blogspot.com Avatar answered Sep 17 '22 20:09

Vijay-Apple-Dev.blogspot.com


I think you may have forgotten to set the userInteractionEnabled property of your buttons to YES which is stopping them receiving events.

As for passing events on to the other view controller, it's a better idea to create a delegate protocol that the your child view controller can use to send messages back to it's parent view controller.

like image 44
Anorak Avatar answered Sep 20 '22 20:09

Anorak