버튼이 있습니다. 사용자가 클릭 할 때마다 응용 프로그램은 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);
}
}
감사합니다.
아 ... 영리합니다. 내가 그것을 보자. 감사. –
흠 ... 'imageview'에 'UIImageView에 대한 표시되지 않는 @interface가 없음'이라는 오류가 발생하여 'addTarget : action : forControlEvents'셀렉터가 선언됩니다. –
@TBlue Damnit, UIControl.If에서 상속 한 클래스의 인스턴스 만 잊어 버렸습니다.이 방법을 계속 사용하려면 UIImageViews를 UIButtons로 변경하고 이미지를 설정하십시오. –