IOS开关的使用
开关用于打开和关闭状态之间的切换。
重要的属性
onImage
offImage
on
重要的方法
- (void)setOn:(BOOL)on animated:(BOOL)animated
添加自定义方法 addSwitch 和开关
-(IBAction)switched:(id)sender{
NSLog(@"Switch current state %@", mySwitch.on ? @"On" : @"Off");
}
-(void)addSwitch{
mySwitch = [[UISwitch alloc] init];
[self.view addSubview:mySwitch];
mySwitch.center = CGPointMake(150, 200);
[mySwitch addTarget:self action:@selector(switched:)
forControlEvents:UIControlEventValueChanged];
}
在 ViewController.m 中修改 viewDidLoad,如下所示
(void)viewDidLoad
{
[super viewDidLoad];
[self addSwitch];
}
输出
现在当我们运行该应用程序我们会看到下面的输出
向右滑动开关输出如下所示