白羽
2018-06-22
来源 :网络
阅读 1527
评论 0
摘要:本文将带你了解iOS开发之获取控制器里面的控件,希望本文对大家学IOS有所帮助。
iOS获取控制器里面的控件
[objc] view plain copy
1. for (int i=0; i<3; i++) {
2.
3. UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(((self.view.frame.size.width - 20)/3 + 10) * i, 100, (self.view.frame.size.width - 20)/3, (self.view.frame.size.width - 20)/3)];
4. testLabel.backgroundColor = [UIColor cyanColor];
5. testLabel.tag=111+i;
6. testLabel.textColor = [UIColor purpleColor];
7. testLabel.text = [NSString stringWithFormat:@"这是第 %d 个",i];
8. [self.view addSubview:testLabel];
9.
10. } //创建 控件UILabel
[objc] view plain copy
1. // 测试按钮
2. UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeCustom];
3. testBtn.frame = CGRectMake((self.view.frame.size.width - 20)/3 + 10, 100 + (self.view.frame.size.width - 20)/3, (self.view.frame.size.width - 20)/3, (self.view.frame.size.width - 20)/3);
4. testBtn.backgroundColor = [UIColor purpleColor];
5. [testBtn addTarget:self action:@selector(testBtnClick) forControlEvents:UIControlEventTouchUpInside];
6. [self.view addSubview:testBtn];
[objc] view plain copy
1. -(void)testBtnClick{
2.
3. for (int i=0; i<3; i++){
4.
5. UILabel *tempLabel=(UILabel *)[self.view viewWithTag:111+i];
6. tempLabel.font=[UIFont systemFontOfSize:12.0];
7. tempLabel.text = [NSString stringWithFormat:@"This is %d",i];
8. tempLabel.textColor = [UIColor redColor];
9. tempLabel.textAlignment = NSTextAlignmentCenter;
10.
11. }//取出 控件UILabel
12.
13. }
[objc] view plain copy
1. //扩展:
2. UIButton *btn1 = (UIButton *)[myView viewWithTag:1];
3. UILabel *Label = (UILabel *)[myView viewWithTag:2];
4. UITextView *TextView = (UITextView *)[myView viewWithTag:3];
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号