【iOS开发】IOS界面开发使用viewWithTag:(int)findTag方法获取界面元素

http://blog.csdn.net/lxp1021/article/details/43952551

今天在开发OS界面的时候,遇到通过界面UIview viewWithTag:(int)findTag选择器定位界面元素的问题,以下把在界面中给元素打Tag,以及通过选择器查找界面元素的代码贴出来,供以后使用:

界面元素打tag

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. //事件监听的问题  
  2.        CGRect btn2Frame = CGRectMake(100.0, 150.0, 60.0, 44.0);  
  3.        //两种不同的方式创建  
  4.        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  5.        btn2.frame =btn2Frame;  
  6.        //设置Title  
  7.        [btn2 setTitle:@"BTN2" forState:UIControlStateNormal];  
  8.        [btn2 setTag:10001];  
  9.        //[btn2 setBackgroundImage:[UIImage imageNamed:@"pic.png"] forState:UIControlStateNormal];  
  10.        [btn2 setBackgroundColor:[UIColor blueColor]];  
  11.        [btn2 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];  
  12.        [self.view addSubview:btn2];  
  13.      
  14.        //事件监听的问题  
  15.        CGRect btn1Frame = CGRectMake(200.0, 150.0, 60.0, 44.0);  
  16.        //两种不同的方式创建  
  17.        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  18.        btn1.frame =btn1Frame;  
  19.        //设置Title  
  20.        [btn1 setTitle:@"BTN1" forState:UIControlStateNormal];  
  21.        [btn1 setTag:10002];  
  22.        //[btn2 setBackgroundImage:[UIImage imageNamed:@"pic.png"] forState:UIControlStateNormal];  
  23.        [btn1 setBackgroundColor:[UIColor blueColor]];  
  24.        [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];  
  25.        [self.view addSubview:btn1];  
  26.      
  27.    CGRect workingFrame;  
  28.      
  29.    workingFrame.origin.x = 15;  
  30.    workingFrame.origin.y = 400;  
  31.    workingFrame.size.width = 140;  
  32.    workingFrame.size.height = 40;  
  33.    for(int i = 0; i < 6; i++)  
  34.    {  
  35.        //UIView *myView = [[UIView alloc] initWithFrame:workingFrame];  
  36.        UIButton *myView =[[UIButton alloc] initWithFrame:workingFrame];  
  37.        [myView setTag:i];//标记方块  
  38.        [myView setBackgroundColor:[UIColor blueColor]];  
  39.        // NSString tit = "Button"i;  
  40.        //NSLog(@"this is Button %d", i);  
  41.        [myView setTitle:@"Button %d" forState:UIControlStateNormal];  
  42.        workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;  
  43.        [myView addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];  
  44.        [self.view addSubview:myView];  
  45.    }  


在方法中使用tag选择器定位元素:

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. -(void)btnPressed:(id)sender{  
    2.     UIButton *thisBtn = (UIButton*)sender;  
    3.     //thisBtn.hidden = YES;  
    4.     //self.view viewWithTag:[00002];  
    5.      UIButton *myButton = (UIButton *)[self.view viewWithTag:(10002)];  
    6.     myButton.hidden = YES;  
    7.     NSLog(@"this button tag is %d",thisBtn.tag);  
    8.     //NSLog(@"self.view subViews %@",self.view.subviews);  
    9.   
    10. //    UIButton* thisBtn = (UIButton *)sender;  
    11. //    [[[UIAlertView alloc] initWithTitle:@"Button" message:[NSString stringWithFormat: @"This is button:%@",thisBtn] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil] show];  
    12. //    NSLog(@"this tag is %d",btn.tag);  
    13. }  
原文地址:https://www.cnblogs.com/paranoia/p/5735918.html