Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward a UIGestureRecognizer from one view controller to another view controller

I'm adding a UIGestureRecognizer to one of my view controllers view.

My desired behaviour is when this gesture is recognised to dismiss the gestures view controller and forward it to another view controller. (the view controller I want to forward it has presented the view controller with the gesture)

Right now when I dismiss the view controller, the pinch gesture recogniser is (logically) failing.

Any suggestions? Thanks.

like image 496
Devfly Avatar asked Nov 24 '25 19:11

Devfly


1 Answers

\EDIT: Now the delegate get the argument with the gestureRecognizer.

You can just implement protocol like:

@protocol ViewControllerWithGestureRecognizerDelegate

- (void)viewControllerGestureRecognizerEvent:(UIPinchGestureRecognizer *)gestureRecognizer;

@end

and add the delegate property to the view controller in which there will be presented gesture recognizer.

@property (nonatomic, weak) id<ViewControllerWithGestureRecognizerDelegate> delegate;

Then add the gesture recognizer to your view Controller:

 UIPinchGestureRecognizer *gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognizerAction:)];
[self addGestureRecognizer:gestureRecognizer];

Call the delegate method in action method of the gesture recognizer:

 - (void)gestureRecognizerAction:(UIPichGestureRecognizer *)gestureRecognizer
 {
   [self.delegate viewControllerGestureRecognizerEvent:gestureRecognizer];
 }

Implement the protocol ViewControllerWithGestureRecognizerDelegate in the second view controller (in which you want to get notified about gesture recognizer events) and set the delegate of the view controller which gesture recognizer to be the second view controller.

 ViewControllerWithGestureRecognizer.delegate = ViewControllerInWhichYouWantToGetNotifiedAboutGestureRecognizerEvents.

This way every time the gesture recognizer will call method on the the one view controller, the second one will be informed about that.

like image 55
Rafał Augustyniak Avatar answered Nov 26 '25 10:11

Rafał Augustyniak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!