如何使用 NSString 标签识别视图?

发布于 2024-12-11 18:13:34 字数 185 浏览 3 评论 0原文

view.tag 只存储 NSInteger 值。

那么,如何用 NSString Value 来标识每个视图呢?

不可用?

一些例子:

view0.stringTag = @"basic";

view1.stringTag = @"advanced";

view.tag is only stored NSInteger Value.

so, how to identify each view with NSString Value?

not avaliable?

some example:

view0.stringTag = @"basic";

view1.stringTag = @"advanced";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

暖阳 2024-12-18 18:13:34

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

@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

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
凉墨 2024-12-18 18:13:34
if use ARC

#import "UIView.h"
#import <objc/runtime.h>

@implementation UIView (StringTagAdditions)
static NSString *kStringTagKey = @"StringTagKey";
- (NSString *)stringTag
{
    return objc_getAssociatedObject(self, CFBridgingRetain(kStringTagKey));
}
- (void)setStringTag:(NSString *)stringTag
{
    objc_setAssociatedObject(self, CFBridgingRetain(kStringTagKey), stringTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
if use ARC

#import "UIView.h"
#import <objc/runtime.h>

@implementation UIView (StringTagAdditions)
static NSString *kStringTagKey = @"StringTagKey";
- (NSString *)stringTag
{
    return objc_getAssociatedObject(self, CFBridgingRetain(kStringTagKey));
}
- (void)setStringTag:(NSString *)stringTag
{
    objc_setAssociatedObject(self, CFBridgingRetain(kStringTagKey), stringTag, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
瑾兮 2024-12-18 18:13:34

不,你不能使用字符串。如果您的目标是代码可读性,则可以使用枚举。不过,请务必从 1 开始枚举,因为 0 是所有视图的默认标记:

typedef enum {
    vtBasic = 1,
    vtAdvanced
} ViewType;
...
view0.tag = vtBasic;

No, you can't use a string. If you are aiming for code readability, you could use an enum. Be sure to start your enum from 1, though, as 0 is the default tag for all views:

typedef enum {
    vtBasic = 1,
    vtAdvanced
} ViewType;
...
view0.tag = vtBasic;
最后的乘客 2024-12-18 18:13:34

使用 恢复标识符

// Supposing iOS 6.0+
view0.restorationIdentifier = @"basic";
view1.restorationIdentifier = @"advanced";

Use restorationIdentifier.

// Supposing iOS 6.0+
view0.restorationIdentifier = @"basic";
view1.restorationIdentifier = @"advanced";
心头的小情儿 2024-12-18 18:13:34

它不被直接支持。当然,您可以创建一个 C 数组、NSArray、NSDictionary 或类似的东西来完成此操作(与 int 标签结合使用)。

it's not supported directly. you could of course create a C array, a NSArray, a NSDictionary, or something like that to accomplish this (in conjunction with the int tag).

薄暮涼年 2024-12-18 18:13:34

仅当您使用 stringTag 属性对自己的 UIView 进行子类化时,这才有可能。你这样做的原因是什么?

This is only possible if you subclass your own UIView with a stringTag property. What is your reason for doing this?

懒猫 2024-12-18 18:13:34

迄今为止最简单的解决方案: 链接到我对类似问题的回答< /a>

尝试设置 UIView 的辅助功能标识符属性:

UIView *view;
view.accessibilityIdentifier = @"your string here";
NSLog(@"%@", view.accessibilityIdentifier);

Output =>你的字符串在这里

这行为就像“标签”属性,但允许你使用 NSString

Simplest solution by far: Link to my answer for a similar question

Try setting the accessibility identifier property of your UIView:

UIView *view;
view.accessibilityIdentifier = @"your string here";
NSLog(@"%@", view.accessibilityIdentifier);

Output => your string here

This behaves just like the "tag" property but allows you to use NSString

韶华倾负 2024-12-18 18:13:34

首先,你永远不会按原样使用标签。总是在 .h 文件中这样做(最佳实践,恕我直言。):

#define MY_VIEW_A 10001
#define MY_VIEW_B 10002

然后,在创建视图时:

view0.tag = MY_VIEW_A
view1.tag = MY_VIEW_B

然后,在您想要找到视图的位置:

UIView *viewA = [mainView viewWithTag:MY_VIEW_A]; //this will be the view0 you created.

或者,您可以定义一个哈希函数,将 NSString 转换为某个 Integer可以指定为标签。例如

- (int) tagForName:(NSString*)name;

- (NSString*) nameForTag:(int)tag;

我也将其留给您定义哈希函数。

First, you never use tags as it is. Always do it like this possibly in a .h file(best practice, IMHO. ):

#define MY_VIEW_A 10001
#define MY_VIEW_B 10002

Then, when creating views:

view0.tag = MY_VIEW_A
view1.tag = MY_VIEW_B

Then, where you want to find the view:

UIView *viewA = [mainView viewWithTag:MY_VIEW_A]; //this will be the view0 you created.

Alternatively, you can define a hash function that converts NSString to some Integer that can be assigned as a tag. E.g.

- (int) tagForName:(NSString*)name;

and then also,

- (NSString*) nameForTag:(int)tag;

I leave it up to you to define the hash function.

罪#恶を代价 2024-12-18 18:13:34

如果你想比较 NSStrings,

if([view0.stringTag isEqualToString:@"basic"]){
  // 
}

就像

if(view0.tag==0){
//
} 

stringTag 是要在视图中定义的 NSString 变量并分配要比较的值一样

If you want to compare NSStrings,

if([view0.stringTag isEqualToString:@"basic"]){
  // 
}

as like

if(view0.tag==0){
//
} 

where stringTag is an NSString variable to be defined in the views and assigned value to compare

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文