我想将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{
}
答案 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)
答案 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)")} }