ios的UIView的tag,当是NSString时

来源:互联网 发布:linux切换图形界面命令 编辑:程序博客网 时间:2024/06/10 10:32

转载自:  http://blog.csdn.net/fjh658/article/details/10048961

使用 hash函数。

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UIView (StringTag)  
  4.   
  5. -(void) stringTag:(NSString*) tag;  
  6. -(UIView*) viewWithStringTag:(NSString*)tag;  
  7. @end  


[cpp] view plaincopy
  1. #import "UIView+StringTag.h"  
  2.   
  3. @implementation UIView (StringTag)  
  4.   
  5. -(void) stringTag:(NSString *)tag  
  6. {  
  7.     [self setTag:[tag hash]];  
  8. }  
  9.   
  10. -(UIView *)viewWithStringTag:(NSString *)tag  
  11. {  
  12.     return [self viewWithTag:[tag hash]];  
  13. }  
  14.   
  15. @end  


 UIView* view = [[UIViewalloc]init];

    [view stringTag:@"ABC"];

    

    [self.view addSubview:view];

    

    UIView* v = [self.view viewWithStringTag:@"ABC"];


0 0