Is it possible to tag a uiimage in uiimageview individually?

此生再无相见时 提交于 2021-01-29 20:11:48

问题


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.


回答1:


That sounds like a bad design, I suggest extend UIImage with your identification property (int or NSString or whatever you want ) and retrieve this property when using it on your NSMutableArray.

This approach works and it is clean




回答2:


All subclasses of UIView have a tag property. A UIImage is not a subclass of UIView, thus it does not inherit a tag property.

If you want to retrieve a particular image later, I would just create another UIImage instance and use the imageNamed: class method to get the image you want. Like so:

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



来源:https://stackoverflow.com/questions/18134014/is-it-possible-to-tag-a-uiimage-in-uiimageview-individually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!