4

我想将 aNSArray作为参数传递给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{

}
4

3 回答 3

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;
}
于 2013-04-19T17:44:54.133 回答
4

您可以使用关联对象将参数与点击手势实例一起传递。

你可以检查这个objective-c-associated-objects

它会解决你的问题。

于 2013-04-19T18:24:02.647 回答
1

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

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

...

func longPress(guesture: UILongPressGestureRecognizer) {
    print("\(guesture.view!.tag)")} }
于 2016-03-19T07:38:11.287 回答