Pass NSDictionary as a parameter to UITapGestureRecognizer

I want to pass NSArray as a UITapGestureRecognizer parameter and access it in the downloadOptionPressed method. How can i do this?

NSArray

 NSArray *parameters = [NSArray arrayWithObjects:currentTrack, nil]; 

Creating a UITapGestureRecognizer

 UITapGestureRecognizer *downloadOptionPressed = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeFrameLabelTapped:)]; [downloadOption addGestureRecognizer:downloadOptionPressed]; 

DownloadOptionPressed Method

 -(void)downloadOptionPressed:(UIGestureRecognizer*)recognizer{ } 
+4
source share
3 answers

Is there a reason why you cannot store information in the owner control controller? Is it for abstraction?

You can always extend the UITapGestureRecognizer to transfer more data:

 @interface UserDataTapGestureRecognizer : UITapGestureRecognizer @property (nonatomic, strong) id userData; @end @implementation UserDataTapGestureRecognizer @end 

...

 UserDataTapGestureRecognizer *downloadOptionPressed = [[UserDataTapGestureRecognizer alloc] initWithTarget:self action:@selector(timeFrameLabelTapped:)]; downloadOptionPressed.userData = parameters; 

...

 - (void)downloadOptionPressed:(UserDataTapGestureRecognizer *)recognizer { NSArray *parameters = recognizer.userData; } 
+10
source

You can use a linked object to pass an argument along with an instance of push gestures.

You can check this objective-c-associated-objects

This will solve your problem.

+4
source

Sometimes with the passage of the index is enough, in this case, the representation of the tag property is your ally. In the following example, I pretended to add a long print to the tableview cell. And as soon as the event was fired, I just wanted to find out which cell was pressed for a long time:

  let longPress = UILongPressGestureRecognizer(target: self, action: "longPress:") cell.tag = indexPath.row cell.addGestureRecognizer(longPress) 

...

 func longPress(guesture: UILongPressGestureRecognizer) { print("\(guesture.view!.tag)")} } 
+1
source

All Articles