How to identify view with NSString tag?

我是研究僧i 提交于 2019-12-06 05:39:48

问题


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";

回答1:


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



回答2:


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



回答3:


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;



回答4:


Use restorationIdentifier.

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



回答5:


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).




回答6:


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




回答7:


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




回答8:


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.




回答9:


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



来源:https://stackoverflow.com/questions/7885955/how-to-identify-view-with-nsstring-tag

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