1.UISwitch的初始化
1 | UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(4.0f, 16.0f, 100.0f, 28.0f)]; |
2.设置UISwitch的初始化状态
1 | switchView.on = YES ; //设置初始为ON的一边 |
3.UISwitch事件的响应
1 | [switchView addTarget: self action: @selector (switchAction:) forControlEvents:UIControlEventValueChang |
.h文件中声明:
UISwitch * leftSwitch;
@property
.m文件中添加:
@synthesize
viewDidUnload()中添加:
leftSwitch
dealloc()中添加:
[leftSwitch
显示:
leftSwitch.hidden
隐藏:
leftSwitch.hidden
通过IBAction方法设置开关
-(IBAction) switchChanged:(id)sender
{
UISwitch
BOOL
[leftSwitch
[rightSwitch
}
iphone 自定义UISwitch
修改UISwitch的标题,实现自定义UISwitch方法有两种:
1.使用类别扩展UISwitch。
如下:
#import
@interface
+ (UISwitch
@property
@property
@end
UISwitch.m文件:
#import
#define TAG_OFFSET
@implementation
- (void) spelunkAndTag: (UIView
{
}
- (UILabel
{
}
- (UILabel
{
return
}
+ (UISwitch
{
int
[switchView
if
{
[switchView.label1
[switchView.label2
}
return
}
@end
2.还有一种方法,这种方法比较简单,但比较难懂,我不甚理解。
UISwitch
((UILabel
((UILabel