UIButton的使用

来源:互联网 发布:windows查看snmp版本 编辑:程序博客网 时间:2024/06/16 13:52

UIButton是按钮视图控件,是UIView的子类,也拥有和view一样的属性,同时也有自己特有的属性;

UIButton的主要作用是用来响应,或控制其他事件的发生;比如说显示,或隐藏其他控件,或调用其他函数方法。


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. UILabel *lable01 = [[UILabel alloc] initWithFrame:CGRectMake(0.00.0300.040.0)];  
  2. lable01.backgroundColor = [UIColor yellowColor];  
  3. lable01.text = @"按钮在控制我的显示,或隐藏";  
  4. lable01.textColor = [UIColor redColor];  
  5. [self.view addSubview:lable01];  
  6. lable01.hidden = YES;  
  7. lable01.center = self.view.center;  
  8. lable01.tag = 2000;  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 实例化  
  2. UIButton *button001 = [[UIButton alloc] initWithFrame:CGRectMake(10.050.0120.040.0)];  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 是view的子类,拥有view一样的属性  
  2. [self.view addSubview:button001];  
  3. //    button001.backgroundColor = [UIColor yellowColor];  
  4. //    button001.layer.cornerRadius = 5.0;  
  5. //    button001.layer.masksToBounds = YES;  
  6. //    button001.layer.borderWidth = 0.5;  
  7. //    button001.layer.borderColor = [UIColor redColor].CGColor;  
  8. button001.tag = 1000;  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 自己特有的属性  
  2. // 1 选中状态,默认是NO,即未选中;通常结合响应方法进行使用  
  3. button001.selected = NO;  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 2 按钮标题颜色,可以根据响应状态进行设置,比如常规,或高亮,或选中等  
  2. [button001 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  
  3. [button001 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];  
  4. [button001 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 3 按钮标题  
  2. // 3-1 大小  
  3. button001.titleLabel.font = [UIFont systemFontOfSize:12.0];  
  4. // 3-2 对齐方式,默认居中  
  5. //    button001.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;  
  6. //    button001.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 4 按钮标题,同样也可以根据响应状态进行设置  
  2. [button001 setTitle:@"显示标签" forState:UIControlStateNormal];  
  3. [button001 setTitle:@"隐藏标签" forState:UIControlStateHighlighted];  
  4. [button001 setTitle:@"隐藏标签" forState:UIControlStateSelected];  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 5 设置图片,默认图标在左,标题在右,还可通过设置改变显示样式,比如图片在上,标题在下,或图片在右,标题在左。  
  2. [button001 setImage:[UIImage imageNamed:@"imageNormal"] forState:UIControlStateNormal];  
  3. [button001 setImage:[UIImage imageNamed:@"imageSelected"] forState:UIControlStateSelected];  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 6 设置背景图片  
  2. [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageNormal"] forState:UIControlStateNormal];  
  3. [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageHighlight"] forState:UIControlStateHighlighted];  
  4. [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageSelected"] forState:UIControlStateSelected];  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 7 其他属性  
  2. // 7-1 如当前显示标题  
  3. NSString *title = button001.currentTitle;  
  4. NSLog(@"title=%@",title);  
  5.       
  6. // 7-2 点击属性,默认为YES,即可点击  
  7. // 方法1  
  8. //    BOOL isEnable = button001.enabled;  
  9. //    NSLog(@"isEnable=%@",@(isEnable));  
  10. //    button001.enabled = NO; // 设置为不可用  
  11. //    isEnable = button001.enabled;  
  12. //    NSLog(@"isEnable=%@",@(isEnable));  
  13. // 方法2 view的userInteractionEnabled属性,默认为YES,即可点击  
  14. BOOL isEnable = button001.userInteractionEnabled;  
  15. NSLog(@"isEnable=%@",@(isEnable));  
  16. button001.userInteractionEnabled = NO// 设置为不可用  
  17. isEnable = button001.userInteractionEnabled;  
  18. NSLog(@"isEnable=%@",@(isEnable));  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // target方法  
  2. [button001 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (void)buttonClick:(UIButton *)button  
  2. {  
  3.     button.selected = !button.selected;  
  4.       
  5.     UILabel *label = (UILabel *)[self.view viewWithTag:2000];  
  6.     label.hidden = !button.selected;  
  7.       
  8.     NSLog(@"按钮被点击了");  
  9. }  




0 0
原创粉丝点击