我需要通过按钮传递字符串数据,例如标记,该怎么办?

[button setTag:tg];


tg是nsstring。也许使用其他属性而不标记?

最佳答案

使用Associated Objects

@interface UIButton (Tagged)

 @property (nonatomic, copy) NSString *tag;

@end




#import <objc/runtime.h>

static const void *tagKey = &tagKey;

@implementation UIButton (Tagged)

- (void)setTag:(NSSting *)tag
{
   objc_setAssociatedObject(self, tagKey, tag, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)tag
{
  return objc_getAssociatedObject(self, tagKey);
}
@end


获取参考associated-objectslink

07-27 21:46