5

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";
bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43

9 Answers9

15

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
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
7
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
akipon
  • 79
  • 1
  • 1
6

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;
jrturton
  • 118,105
  • 32
  • 252
  • 268
1

Use restorationIdentifier.

// Supposing iOS 6.0+
view0.restorationIdentifier = @"basic";
view1.restorationIdentifier = @"advanced";
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
0

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

justin
  • 104,054
  • 14
  • 179
  • 226
0

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

Dries De Smet
  • 876
  • 8
  • 16
-1

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

Community
  • 1
  • 1
Max Friedman
  • 445
  • 4
  • 15
-2

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.

Community
  • 1
  • 1
zakishaheen
  • 5,551
  • 1
  • 22
  • 29
-3

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

SriPriya
  • 1,240
  • 15
  • 22