UISwitch用法-以及-自定义UISwitch控件

@UISwitch用法

一、第一种创建UISwitch控件的方法,在代码中动态创建。

1、打开Xcode  4.3.2, 新建项目Switch,选择Single View Application。

2、打开ViewController.m文件在viewDidLoad方法里添加代码:

[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 20, 10)];  
  5.     [switchButton setOn:YES];  
  6.     [switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];  
  7.     [self.view addSubview:switchButton];  
  8.       
  9.     // Do any additional setup after loading the view, typically from a nib.  
  10. }  

[switchButton addTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];

代码中selector中的switchAction:需要我们自己实现,就是按下时接收到的事件。

记得把switchButton加到当前view,调用[self.viewaddSubview:switchButton];

3、监听UISwitch按下事件

实现代码如下:

[cpp]  view plain copy
  1. -(void)switchAction:(id)sender  
  2. {  
  3.     UISwitch *switchButton = (UISwitch*)sender;  
  4.     BOOL isButtonOn = [switchButton isOn];  
  5.     if (isButtonOn) {  
  6.         showSwitchValue.text = @"是";  
  7.     }else {  
  8.         showSwitchValue.text = @"否";  
  9.     }  
  10. }  

showSwitchValue是我通过拖拽控件方法放到界面上的Label,方便显示效果

运行,效果:

UISwitch用法-以及-自定义UISwitch控件_第1张图片



二、通过拖拽方法使用UISwitch

1、往xib文件上拖拽一个UISwitch控件。

UISwitch用法-以及-自定义UISwitch控件_第2张图片

2、按alt+command + return键开启Assistant Editor模式,选中UISwitch控件,按住Control键,往ViewController.h拖拽

UISwitch用法-以及-自定义UISwitch控件_第3张图片

3、选Action方式

UISwitch用法-以及-自定义UISwitch控件_第4张图片

4、.m文件中实现switchAction 。刚才动态创建的时候也用到这个方法名称,可以先注释掉刚才的。

[cpp]  view plain copy
  1. - (IBAction)switchAction:(id)sender {  
  2.     UISwitch *switchButton = (UISwitch*)sender;  
  3.     BOOL isButtonOn = [switchButton isOn];  
  4.     if (isButtonOn) {  
  5.         showSwitchValue.text = @"是";  
  6.     }else {  
  7.         showSwitchValue.text = @"否";  
  8.     }  
  9. }  


@自定义UISwitch

1.使用类别扩展UISwitch。 
如下:
 下面是UISwitch.h文件:
#import <UIKit/UIKit.h>
 
@interface UISwitch (tagged)
+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;
@property (nonatomic, readonly) UILabel *label1;
@property (nonatomic, readonly) UILabel *label2;
@end
 
UISwitch.m文件:
#import "UISwitch-Extended.h"
 
#define TAG_OFFSET 900
 
@implementation UISwitch (tagged)
- (void) spelunkAndTag: (UIView *) aView withCount:(int *) count
{
 for (UIView *subview in [aView subviews])
 {
 if ([subview isKindOfClass:[UILabel class]])
 {
 *count += 1;
 [subview setTag:(TAG_OFFSET + *count)];
 }
 else 
 [self spelunkAndTag:subview withCount:count];
 }
}
 
- (UILabel *) label1 

 return (UILabel *) [self viewWithTag:TAG_OFFSET + 1]; 
}
 
- (UILabel *) label2 

return (UILabel *) [self viewWithTag:TAG_OFFSET + 2]; 
}
 
+ (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2
{
 UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
 
int labelCount = 0;
[switchView spelunkAndTag:switchView withCount:&labelCount];
 
if (labelCount == 2)
{
[switchView.label1 setText:tag1];
[switchView.label2 setText:tag2];
}
 
return [switchView autorelease];
}
 
@end
 
2.还有一种方法,这种方法比较简单,但比较难懂,我不甚理解。

UISwitch *isFooOrBar=[[UISwitch alloc] init];
 

((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:0]).text = @"Foo";
((UILabel *)[[[[[[isFooOrBar subviews] lastObject] subviews] objectAtIndex:2] subviews]objectAtIndex:1]).text = @"Bar";

你可能感兴趣的:(UI,UISwitch)