首页 技术 正文
技术 2022年11月13日
0 收藏 309 点赞 4,999 浏览 2476 个字

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

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

界面元素打tag

  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. CGRect btn1Frame = CGRectMake(200.0, 150.0, 60.0, 44.0);
  15. //两种不同的方式创建
  16. UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  17. btn1.frame =btn1Frame;
  18. //设置Title
  19. [btn1 setTitle:@”BTN1″ forState:UIControlStateNormal];
  20. [btn1 setTag:10002];
  21. //[btn2 setBackgroundImage:[UIImage imageNamed:@”pic.png”] forState:UIControlStateNormal];
  22. [btn1 setBackgroundColor:[UIColor blueColor]];
  23. [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
  24. [self.view addSubview:btn1];
  25. CGRect workingFrame;
  26. workingFrame.origin.x = 15;
  27. workingFrame.origin.y = 400;
  28. workingFrame.size.width = 140;
  29. workingFrame.size.height = 40;
  30. for(int i = 0; i < 6; i++)
  31. {
  32. //UIView *myView = [[UIView alloc] initWithFrame:workingFrame];
  33. UIButton *myView =[[UIButton alloc] initWithFrame:workingFrame];
  34. [myView setTag:i];//标记方块
  35. [myView setBackgroundColor:[UIColor blueColor]];
  36. // NSString tit = “Button”i;
  37. //NSLog(@”this is Button %d”, i);
  38. [myView setTitle:@”Button %d” forState:UIControlStateNormal];
  39. workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;
  40. [myView addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
  41. [self.view addSubview:myView];
  42. }

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

    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. //    UIButton* thisBtn = (UIButton *)sender;
    10. //    [[[UIAlertView alloc] initWithTitle:@”Button” message:[NSString stringWithFormat: @”This is button:%@”,thisBtn] delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:@”OK”, nil] show];
    11. //    NSLog(@”this tag is %d”,btn.tag);
    12. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297