本文介绍了如何用NSString标签识别视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

view.tag仅存储NSInteger值。

view.tag is only stored NSInteger Value.

那么,如何识别具有NSString值的每个视图?

so, how to identify each view with NSString Value?

不可用?

一些例子:

view0.stringTag = @"basic";

view1.stringTag = @"advanced";


推荐答案

没有 stringTag 属性 UIView 。如果您需要执行此类操作,可以在 UIView 上使用类别,并将标记存储在关联对象中:

There's no stringTag property on UIView. If you need to do this kind of thing you can use a category on UIView and store the tag in an associated object:

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

@implementation UIView (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

这篇关于如何用NSString标签识别视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 20:29