以下のコードを使用しています
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];
- (void) processTap
{
//do something
}
しかし、データを processTap 関数に送信する必要があります。とにかくこれを行うことはありますか?
以下のコードを使用しています
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];
- (void) processTap
{
//do something
}
しかし、データを processTap 関数に送信する必要があります。とにかくこれを行うことはありますか?
例:
UIImageView *myPhoto = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tab-me-plese.png"]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap:)];
[myPhoto setTag:1]; //set tag value
[myPhoto addGestureRecognizer:tap];
[myPhoto setUserInteractionEnabled:YES];
- (void)processTap:(UIGestureRecognizer *)sender
{
NSLog(@"tabbed!!");
NSLog(@"%d", sender.view.tag);
}
iOS - UITapGestureRecognizer - Selector with Argumentsに同様の質問に対する適切な回答がありますが、ビューにアタッチしたくないデータを渡したい場合は、必要なデータにアクセスできる 2 つ目の関数を作成することをお勧めします. 例えば:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(passDataToProcessTap)];
- (void)passDataToProcessTap {
[self processTapWithArgument:self.infoToPass];
// Another option is to use a static variable,
// Or if it's not dynamic data, you can just hard code it
}
- (void) processTapWithArgument:(id)argument {
//do something
}