将NSDictionary作为参数传递给UITapGestureRecognizer

时间:2013-04-19 17:36:20

标签: ios objective-c nsarray uitapgesturerecognizer

我想将NSArray作为参数传递给UITapGestureRecognizer,并在downloadOptionPressed方法中访问它。我怎么能这样做?

NSArray

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

创建UITapGestureRecognizer

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

downloadOptionPressed方法

-(void)downloadOptionPressed:(UIGestureRecognizer*)recognizer{

}

3 个答案:

答案 0 :(得分:11)

是否有理由无法将信息存储在拥有的视图控制器中?是抽象吗?

您可以随时扩展UITapGestureRecognizer以携带更多数据:

@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;
}

答案 1 :(得分:4)

您可以使用关联对象来传递参数以及点按手势实例。

您可以查看objective-c-associated-objects

它将解决您的问题。

答案 2 :(得分:1)

有时传递索引就足够了,在这种情况下,tag属性视图就是你的盟友。在下面的例子中,我假装在tableview单元格中添加长按。一旦事件被触发,我只想知道长按哪个细胞:

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

...

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