这一篇是在上一篇代码的基础上对代码进行的修改,先看效果吧
左上角那个控件是一个按钮,当点击的时候,触发点击事件,对中间的label上的文字进行修改。下面是部分代码:
//
// ViewController.m
// ViewTest
//
// Created by Moluth on 17/4/11.
// Copyright (c) 2017年 Moluth. All rights reserved.
//
#import "ViewController.h"
@interface ViewController (){
int count;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 60)];//创建一个UILabel
[self.view addSubview:label];//添加label为子view
label.backgroundColor=[UIColor redColor];
label.text=@"召唤效果";
label.textColor=[UIColor whiteColor];//设置文字颜色为白色
label.textAlignment=NSTextAlignmentCenter;//居中显示
label.font=[UIFont systemFontOfSize:40 weight:60];//设置字体
label.shadowColor=[[UIColor alloc] initWithRed:0.7 green:0.7 blue:0.7 alpha:0.7];//设置阴影灰色
label.shadowOffset=CGSizeMake(2, 2);//设置阴影偏移值
//设置圆角
label.layer.cornerRadius=8;
label.layer.masksToBounds=YES;
//设置位置
label.center=self.view.center;
//设置标签
label.tag=123;
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];//创建button
button.frame=CGRectMake(0, 0, 100, 40);//设置按钮位置和大小
[self.view addSubview:button];//添加button
[button setTitle:@"按钮" forState:UIControlStateNormal];//设置标题
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置标题颜色
[button setBackgroundColor:[UIColor orangeColor]];//设置背景颜色
//设置圆角
button.layer.cornerRadius=8;
button.layer.masksToBounds=YES;
//设置点击事件UIControlEventTouchUpInside,处理该事件的对象是当前对象,使用click方法进行处理,于是在下面定义了click方法
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click{
//之前给label设置了一个标签123,这里根据这个标签获取label对象,并对其进行修改
UILabel *label=[self.view viewWithTag:123];
label.text=[[NSString alloc] initWithFormat:@"点击%d",count++ ];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end