UIButton的使用
来源:互联网 发布:windows查看snmp版本 编辑:程序博客网 时间:2024/06/16 13:52
UIButton是按钮视图控件,是UIView的子类,也拥有和view一样的属性,同时也有自己特有的属性;
UIButton的主要作用是用来响应,或控制其他事件的发生;比如说显示,或隐藏其他控件,或调用其他函数方法。
- UILabel *lable01 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 40.0)];
- lable01.backgroundColor = [UIColor yellowColor];
- lable01.text = @"按钮在控制我的显示,或隐藏";
- lable01.textColor = [UIColor redColor];
- [self.view addSubview:lable01];
- lable01.hidden = YES;
- lable01.center = self.view.center;
- lable01.tag = 2000;
-
- UIButton *button001 = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 50.0, 120.0, 40.0)];
-
- [self.view addSubview:button001];
-
-
-
-
-
- button001.tag = 1000;
-
- [button001 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- [button001 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
- [button001 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
-
-
- button001.titleLabel.font = [UIFont systemFontOfSize:12.0];
-
-
-
-
- [button001 setTitle:@"显示标签" forState:UIControlStateNormal];
- [button001 setTitle:@"隐藏标签" forState:UIControlStateHighlighted];
- [button001 setTitle:@"隐藏标签" forState:UIControlStateSelected];
-
- [button001 setImage:[UIImage imageNamed:@"imageNormal"] forState:UIControlStateNormal];
- [button001 setImage:[UIImage imageNamed:@"imageSelected"] forState:UIControlStateSelected];
-
- [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageNormal"] forState:UIControlStateNormal];
- [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageHighlight"] forState:UIControlStateHighlighted];
- [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageSelected"] forState:UIControlStateSelected];
-
-
- NSString *title = button001.currentTitle;
- NSLog(@"title=%@",title);
-
-
-
-
-
-
-
-
-
- BOOL isEnable = button001.userInteractionEnabled;
- NSLog(@"isEnable=%@",@(isEnable));
- button001.userInteractionEnabled = NO;
- isEnable = button001.userInteractionEnabled;
- NSLog(@"isEnable=%@",@(isEnable));
-
- [button001 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
- - (void)buttonClick:(UIButton *)button
- {
- button.selected = !button.selected;
-
- UILabel *label = (UILabel *)[self.view viewWithTag:2000];
- label.hidden = !button.selected;
-
- NSLog(@"按钮被点击了");
- }
0 0