How to detect touch event in child viewcontroller in ios
Asked Answered
F

3

7

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.

Fugazy answered 16/7, 2014 at 11:21 Comment(5)
Can you use same open source library for drower? I used RESideMenu. It has some interesting effect and has all tap handling build in.Granddaddy
No, I had an option but I don't want to use any third party libraries :)Fugazy
buttons will cancel out tap event...disable thir userenteraction when half closedUntrimmed
add a view of frame with rect 320x height over your child viewcontroller when half closed. add tap over this view.Untrimmed
You can take the help of delegate method. Can you show the code about how you are adding the side menu to the parent controller, so i can help you out with the code you can use here.Demarche
P
7

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");
}
Poteen answered 16/7, 2014 at 11:42 Comment(0)
A
1

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.

Asbury answered 16/7, 2014 at 11:31 Comment(0)
G
0

If you are missing touch events for buttons in the subviews, try adding target for the button.

[button addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:UIControlEventTouchUpInside];

To notify parentViewcontroller Post you notification from your selector function in childViewController.

[[NSNotificationCenter defaultCenter] postNotificationName:@"notificaiton_name" object:object_you_want_to_send];

And in your parent view controller add observer for the NSNotificationCenter

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method_you_want_to_call:) name:@"notificaiton_name" object:nil];
Greenback answered 16/7, 2014 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.