UISwitch *swt = [[UISwitch alloc] initWithFrame:CGRectMake(0, 30, 100, 50)];
UIImage *on = [UIImage imageNamed:@"smiley_grin_30"];
UIImage *off = [UIImage imageNamed:@"smiley_sad_30"];
//設置on和off圖片,7無效
swt.onImage = on;
swt.offImage = off;
[swt setOn:YES animated:YES];
[swt addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
- (void)change:(UISwitch *)swt
{
//獲取開關狀態
//swt.isOn == YES == on
//swt.isOn == NO == off
if (swt.isOn)
{
NSLog(@"開");
}
else
{
NSLog(@"關");
}
}