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";
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";
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
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
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;
// Supposing iOS 6.0+
view0.restorationIdentifier = @"basic";
view1.restorationIdentifier = @"advanced";
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).
This is only possible if you subclass your own UIView with a stringTag property. What is your reason for doing this?
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
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.
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