IOS: add @selector parameter

When i have this line of code

UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:)]; 

and this one

 - (void)dragGestureChanged:(UILongPressGestureRecognizer*)gesture{ ... } 

I want to add to the "@selector (dragGestureChanged :)" parameter, which is the "(UIScrollView *) scrollView", how can I do this?

+8
ios parameters xcode
source share
2 answers

You cannot directly - UIGestureRecognizer know how to issue a call to a selector that takes only one argument. To be completely general, you probably want to go through a block. Apple did not build this, but it is quite easy to add, at least if you want to subclass the gesture recognizers that you want to get around the problem of adding a new property and cleaning it properly, without delving deep into the runtime.

So, for example, (recorded as I go, unverified)

 typedef void (^ recogniserBlock)(UIGestureRecognizer *recogniser); @interface UILongPressGestureRecognizerWithBlock : UILongPressGestureRecognizer @property (nonatomic, copy) recogniserBlock block; - (id)initWithBlock:(recogniserBlock)block; @end @implementation UILongPressGestureRecognizerWithBlock @synthesize block; - (id)initWithBlock:(recogniserBlock)aBlock { self = [super initWithTarget:self action:@selector(dispatchBlock:)]; if(self) { self.block = aBlock; } return self; } - (void)dispatchBlock:(UIGestureRecognizer *)recogniser { block(recogniser); } - (void)dealloc { self.block = nil; [super dealloc]; } @end 

And then you can just do:

 UILongPressGestureRecognizer = [[UILongPressGestureRecognizerWithBlock alloc] initWithBlock:^(UIGestureRecognizer *recogniser) { [someObject relevantSelectorWithRecogniser:recogniser scrollView:relevantScrollView]; }]; 
+9
source share

Thus, the method will look like this:

 - (void)dragGestureChanged:(UILongPressGestureRecognizer*)gesture scrollView:(UIScrollView *)scrollview { ... } 

The selector will look like this:

 UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:scrollView:)]; 
+3
source share

All Articles