0

ボタンがあります。ユーザーがクリックするたびに、アプリケーションはUIImageView (imageview) をインスタンス化し、そのタグに一意の番号を割り当てます。

- (IBAction)buttonClicked:(id)sender {
    imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 240)];
    NSUInteger currentag = [self UniqueNum]; // Assigning a unique number to the tag variable
    [self.view addSubview:imageview];
    imageview.backgroundColor = [UIColor blueColor];
    imageview.userInteractionEnabled = YES;
    imageview.tag = currentag;
}

私の目標は、ユーザーが触れた UIImageView コピーのタグ番号を取得することです。次のコードで、実際に取得するのは、アプリケーションが最後に作成した UIImageView コピーのタグ番号です。アプリケーションがタッチされたオブジェクトのタグ番号を指すようにするにはどうすれば改善できますか?

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    if([touch view] == imageview) {
        NSLog(@"Tag: %i",imageview.tag);
    }    
}

ご協力ありがとうございました。

4

1 に答える 1

1

インタラクションが必要な場合は、UIImageViews ではなく、カスタム イメージで UIButtons を使用することをお勧めします。

ボタンを追加するときにこれを追加します。

   [imageview addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventAllTouchEvents]; 

sender次に、 in someMethod(宣言) を UIView にキャストして、そのタグを取得します。

ところで、someMethod のメソッド シグネチャは次のようになります。

-(void) someMethod:(id)sender

オプションでイベント パラメータを追加することもできますが、ここでは必要ないようです。

于 2013-04-12T23:24:57.410 に答える