0

This is a trick I use a lot in android:

  • I take an image with Image Picker: camera or gallery
  • I put the image in an ImageView
  • I also put the image url in the ImageView, in the tag field
  • so later when I need the url of an image (to send to server), I grab it from the imageView's tag.

But tag in iOS is a bit different from android: in iOS it's just a number. So is there such a way of piggybacking on an UIButton on iOS: basically any field whatsoever that is available for storing a text and which the user cannot see?

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

3 Answers3

1

Natively, there's no way to do this. However, you can use a category and store the text in an associated object:

@interface UIImageView (StringTagAdditions)
@property (nonatomic, copy) NSString *stringTag;
@end

@implementation UIImageView (StringTagAdditions)
static NSString *kStringTagKey = @"StringTagKey";

- (NSString *)stringTag {
    return objc_getAssociatedObject(self, kStringTagKey);
}

- (void)setStringTag:(NSString *)stringTag {
    objc_setAssociatedObject(self, kStringTagKey, stringTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end

More info @ http://nshipster.com/associated-objects/

rocky
  • 3,521
  • 1
  • 23
  • 31
1

This type of thing has been asked before, but you could pull this off with a UIButton category that adds getter/setter like methods that store this value for you:

Or you could subclass UIButton and add the property you need. There are lots of options, but I'm unclear on what you mean by "the user cannot see"?

Community
  • 1
  • 1
Aaron
  • 7,055
  • 2
  • 38
  • 53
  • I remember a computer science professor saying that it's not good to add category to classes I (i.e. my team) didn't create) – Katedral Pillon Jul 29 '14 at 00:32
  • No sweat, not sure I agree with your professor though. I also mentioned that you could subclass `UIButton`, :-) – Aaron Jul 29 '14 at 01:40
1

I'm not aware of an analogous field on a UIImageView. Your best bet may be to subclass UIImageView to add such a property. In the .h file for the new class, do something like:

@interface SubclassedUIImageView : UIImageView

@property (strong, nonatomic, copy) NSString *url;

@end

Then assign the url value to SubclassedUIImageView in imagePickerController: didFinishPickingMediaWithInfo:. Assuming you're using Interface Builder, to use the subclassed UIImageView, you drop a UIImageView control onto the parent view, go to the Identity inspector, and change the Custom Class field to the name of your subclassed UIImageView.

Keith Kurak
  • 662
  • 1
  • 6
  • 11