Passing multiple tags with UIButton

微笑、不失礼 提交于 2019-12-31 02:57:15

问题


OK I have a specific situation. I am using a custom class to create some buttons and I can set their tag property with unique numbers like:

button.tag =[NSNumber numberWithInt:[10]];

This is very useful in another part of my program because I can access this unique tag like:

UIButton *clicked= (UIButton *) sender;

ButtonTag = [NSString stringWithFormat:@"%d", clicked.tag];

Now I want to pass one more unique property just like this. I am making this up but this is how I envision it

button.tagCREATED_BY_ME =[NSNumber numberWithInt:[9000]];

The question might be poorly worded but I don't know any better so I called it "tag".(correct wording might be element/property etc) How do I create a similar property to function just like .tag?

Thanks a lot! arda


回答1:


In your subclassed/custom button, you can add a string property or even an integer property whichever you feel good.

@interface CustomButton: ....
       ...
       @property(strong) NSString *createdBy;

@end

Then you can access those as aButton.createdBy




回答2:


What do you want to achieve?

There is the possibility of adding an associative references. The good part about this, is that you don't need to sub-class it. So, start by creating a Category for the UIButton:

@interface UIButton (ExtraTag)

@property (nonatomic, retain) id extraTag;

@end

And the .m:

static char const * const ExtraTagKey = "ExtraTag";

@implementation UIButton (ExtraTag)
@dynamic extraTag;

- (id)extraTag {
    return objc_getAssociatedObject(self, ExtraTagKey);
}

- (void)setExtraTag:(id)newExtraTag {
    objc_setAssociatedObject(self, ExtraTagKey, newExtraTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

You can check the article I used.




回答3:


CALayer allows Key-Value coding actually.

You can literally just do this (on any UI object):

[button.layer setValue:@(9000) forKey:@"tagCREATED_BY_ME"];

and to read it just use

[button.layer valueForKey:@"tagCREATED_BY_ME"]




Obligatory, the above is all you need to get this up and going, you're good to go.

For others, more advanced /or/ specific stuff follows:

If you need these keys to have a default value when nothing has yet been assigned to them... You can set these custom "tags" (eh) to have default return values if you name them according to a pattern. For example I start all of my layer keys name's with "customKey_". So the above would have been @"customKey_tagCREATED_BY_ME", then you can have your .m file return the default key values for any standard key like masksToBounds but then return a very specific value for your keys (aka keys that start with "customKey_") with the following method:

+(id)defaultValueForKey:(NSString *)key {
    if ([key hasPrefix:@"customKey_"]) {
        return @(0);
    }
    return [CALayer defaultValueForKey:key];
}

The reason you have to have a naming pattern (like always having the prefix "customKey_") is so you don't interfere with a CALayer's natural properties like .size and .backgroundColor, etc. Your default value you want returned will only be returned on properties (key) starting with "customKey_" or whatever naming pattern you use.




回答4:


You can also use Associated references instead of tags manipulation

#import <objc/runtime.h>

static char kThumbnailButtonAssociatedPhotoKey;

// ...

- (void)setAssociatedPhoto:(Photo *)associatedPhoto
        forThumbnailButton:(UIButton *)thumbnailButton
{
    objc_setAssociatedObject(thumbnailButton,
                             &kThumbnailButtonAssociatedPhotoKey,
                             associatedPhoto,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (Photo *)associatedPhotoForThumbnailButton:(UIButton *)thumbnailButton
{
    return objc_getAssociatedObject(thumbnailButton,
                                    &kThumbnailButtonAssociatedPhotoKey);
}

Now we can easily set/get the associated photo for a button:

- (void)configureThumbnailButtonForPhoto:(Photo *)photo
{
    // ...
    [self setAssociatedPhoto:photo
          forThumbnailButton:thumbnailButton];
    // ...
}

- (void)thumbnailButtonTapped
{
    Photo *photo = [self associatedPhotoForThumbnailButton:thumbnailButton];
    // ...
}

Blog post about tags and associated references




回答5:


You can subclass UIButton.

In your subclass, add a new property:

@property (strong, nonatomic) NSNumber *tagCREATED_BY_ME;



回答6:


You could look into KVC.

Or if you wanted to stick to the KISS principle - subclass UIButton and create a property.



来源:https://stackoverflow.com/questions/15943648/passing-multiple-tags-with-uibutton

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