//7.0着色
//父视图着色改变 那么上面的所有的子视图着色都会改变
self.window.tintColor = [UIColor redColor];
//7.0无边框按钮
for (int i=0; i<7 ;i++) {
UIButton *b =[UIButton buttonWithType:i];
b.frame = CGRectMake(50, i*50+20, 100, 50) ;
if (i == 4) {
//dark button
b.tintColor =[UIColor grayColor];
}
[self.window addSubview:b];
}
//自定义按钮
// UIButton *b1 = [UIButton buttonWithType:UIButtonTypeCustom];
// b1.frame = CGRectMake(200, 200, 50, 50);
// b1.layer.cornerRadius =25.0;
// b1.layer.borderWidth = 2.0;
// b1.layer.borderColor =[UIColor grayColor].CGColor;
// [b1 setTitle:@"9" forState:UIControlStateNormal];
// [b1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//
// [self.window addSubview:b1];
UIImageView * imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
//iOS对png格式的图片支持较好
imgView.image =[UIImage imageNamed:@"640*1136"];
[self.window addSubview:imgView];
[imgView release];
//将10个按钮放在UIImageView上
int k = 1;
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
UIColor *color = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
UIButton *b1 = [UIButton buttonWithType:UIButtonTypeCustom];
b1.frame = CGRectMake(20 +100*j, 90+100*i, 80, 80);
b1.layer.cornerRadius =40;
b1.layer.borderColor= [UIColor grayColor].CGColor;
b1.layer.borderWidth =3.0;
NSString *str = [NSString stringWithFormat:@"%d",k++];
[b1 setTitle:str forState:UIControlStateNormal];
//事件传参
[b1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[b1 setTitleColor:color forState:UIControlStateNormal];
b1.titleLabel.font = [UIFont systemFontOfSize:40];
[imgView addSubview:b1];
//UIImageView默认是不能接受点击事件的
imgView.userInteractionEnabled = YES;
}
}
UIButton *b1 = [UIButton buttonWithType:UIButtonTypeCustom];
_num = [[NSMutableString alloc] init];
b1.frame = CGRectMake(120, 390, 80, 80);
b1.layer.cornerRadius =40;
b1.layer.borderColor= [UIColor grayColor].CGColor;
b1.layer.borderWidth =3.0;
[b1 setTitle:@"0" forState:UIControlStateNormal];
//添加事件
[b1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
//移除事件
[b1 removeTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[b1 setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateNormal];
b1.titleLabel.font = [UIFont systemFontOfSize:40];
[imgView addSubview:b1];
label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 30, 280, 50)] retain];
label.backgroundColor = [UIColor grayColor];
label.textAlignment = NSTextAlignmentCenter;
label.font =[UIFont systemFontOfSize:40];
[self.window addSubview:label];
return YES;
}
//用户点击了哪个按钮,就把哪个按钮作为参数传递过来 sender
/*监听用户的点击事件 每次点击 代码从该方法开始执行
用户每一次点击都是一个事件循环 RunLoop
每一个时间循环都会自动加上自动释放池
事件循环结束后 系统会回收自动释放池中的对象
*/
- (void)click:(id)sender
{
NSLog(@"sender is %@",sender);
//获取按钮的标题
// NSLog(@"%@",[sender titleForState:UIControlStateNormal]);
// [_num appendString:[sender titleForState:UIControlStateNormal]];
// label.text =_num;
// label.font = [UIFont systemFontOfSize:40];
// label.textAlignment = NSTextAlignmentCenter; 或者
if (label.text.length ==0) {
label.text =[sender titleForState:UIControlStateNormal];
}else{
if (label.text.length>10) {
NSLog(@"这是火星的号码吗?");
}
NSString *str = label.text;
//该方法中str不能为nil
NSString *newStr = [str stringByAppendingString:[sender titleForState:UIControlStateNormal]];
label.text = newStr;
}
}