【发布时间】:2011-10-01 15:23:45
【问题描述】:
我使用UITapGestureRecognizer 是因为我使用UIScrollView 作为UILabels 的容器。基本上我正在尝试使用带有参数的操作方法,以便我可以例如将myLabel.tag 值发送到action 方法,以根据点击触发的UILabel 来了解要执行的操作。
一种方法是拥有与UILabels 一样多的操作方法,但这在代码方面并不是很“漂亮”。我想要实现的只是一个带有 switch 语句的操作方法。
这可能吗,还是我必须这样做(叹气):
UITapGestureRecognizer *myLabel1Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabel1Tap)];
[myLabel1Tap addGestureRecognizer:myLabel1Tap];
UITapGestureRecognizer *myLabel2Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabel2Tap)];
[myLabel1Tap addGestureRecognizer:myLabel2Tap];
UITapGestureRecognizer *myLabelNTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myLabelNTap)];
[myLabel1Tap addGestureRecognizer:myLabelNTap];
- (void)myLabel1Tap {
// Perform action
}
- (void)myLabel2Tap {
// Perform action
}
- (void)myLabelNTap {
// Perform action
}
【问题讨论】:
标签: ios objective-c uiscrollview uilabel uitapgesturerecognizer