0

I searched in the class reference of UIImageView and UIImage, but did not find any info on tagging.

In my code, I tagged all of the images in my NSMutableArray of images for my UIImageView, so I could retrieve the tag later:

[[images objectAtIndex:0] setTag:1];
[[images objectAtIndex:1] setTag:2];
[[images objectAtIndex:2] setTag:3];
...

This causes my application to crash when I run it.

The output reads:

2013-08-08 11:26:48.596 Tap Me[8145:c07] -[UIImage setTag:]: unrecognized selector sent to instance 0x752e820 2013-08-08 11:26:48.597 Tap Me[8145:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage setTag:]: unrecognized selector sent to instance 0x752e820' * First throw call stack: (0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0x2d5c 0xf51c7 0xf5232 0x100c25 0x3003a3 0xfdee3 0xfe167 0xfe1a7 0x46a0a2 0x45bb99 0x45bc14 0x10e3705 0x172c0 0x17258 0xd8021 0xd857f 0xd76e8 0x46cef 0x46f02 0x24d4a 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x267d 0x25a5) libc++abi.dylib: terminate called throwing an exception (lldb)

I later need to access whether the tag of the image currently being displayed in UIImageView has a tag of 10 or less, or above 10:

 if ((picImageView.tag<=10 && [[sender currentTitle]isEqual:@"soap"])||    (picImageView.tag>10 && [[[sender titleLabel] text]isEqual:@"cheese"]))
    {
        count++;
        [picImageView setImage:[images objectAtIndex:arc4random()%imageNames.count]];
    }
else
    {
        [picImageView setImage:[images objectAtIndex:arc4random()%imageNames.count]];
    }

I am new to objective c. If anyone could reply with code showing how to add a tag to a UIImage, that would help me greatly. Thank you in advance and sorry for the silly question.

4

2 回答 2

2

这听起来像是一个糟糕的设计,我建议使用您的标识属性(int 或 NSString 或任何您想要的)扩展 UIImage,并在您的 NSMutableArray 上使用它时检索此属性。

这种方法有效,而且很干净

于 2013-08-08T19:36:17.890 回答
2

的所有子类UIView都有一个tag属性。AUIImage不是 的子类UIView,因此它不继承tag属性。

如果您想稍后检索特定图像,我只需创建另一个UIImage实例并使用imageNamed:类方法来获取您想要的图像。像这样:

UIImage *yourImage = [UIImage imageNamed:@"yourImage.png"];

于 2013-08-08T19:03:59.027 回答