How to detect touch event in child view controller in ios

I created a menu for the iPhone, for which I used the concept of a container ship to add a child view controller to the parent view controller. When the user presses the menu button on the navigation bar, I just change the frame of the child view controller to animate it like a drop-down menu so that the child view controller cuts half of its frame to the right.

Now my problem is whenever a user clicks on any part of the child view controller, I just want to notify his parent view controller that touched the event fired on his child view controller so that the parent view controller can reset the child frame to animate it how to close half of the opened menu.

I used tap gestures and added it to the view of each child of the child views. therefore, a click gesture on any of the previews will notify the parent view controller of the touch event.

A touch event works fine for the main view of the child view controller, but any click on any button does not recognize the TAP event.

I do not know where I am wrong. please help me how to notify the parent view controller of any touches in its child view controller. Thanks in advance.

+6
source share
3 answers

You shouldn't do that

"I used tap gestures and added it to each child view of the child views in the subviews. "

After opening the menu, you should add one overlay view on top of the parentviewcontroller view. therefore, he will remain at the top of the left and right views.

you must add a tap recognizer on it to destroy / hide the overlay view and make the Hide menu animation there. See 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"); } 
+7
source

I think you may have forgotten to set the userInteractionEnabled property of your buttons to YES , which stops them when events are received.

Regarding the transfer of events to another view controller, it’s best to create a delegate protocol that your child view controller can use to send messages back to the parent view controller.

+1
source

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

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

To notify parentViewcontroller You will send a notification from your selection function to childViewController.

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

And in the parent view controller add an observer for NSNotificationCenter

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(method_you_want_to_call:) name:@"notificaiton_name" object:nil]; 
0
source

Source: https://habr.com/ru/post/972316/


All Articles