10

以下のコードを使用しています

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];

- (void) processTap
{
//do something
}

しかし、データを processTap 関数に送信する必要があります。とにかくこれを行うことはありますか?

4

2 に答える 2

30

例:

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);    
}
于 2012-11-28T06:30:25.060 に答える
3

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
}
于 2014-09-25T16:16:24.617 に答える