如何设置开关on/off状态iOS?

How to set switch on/off state iOS?

我尝试使用 [self.switchAutoPlay setOn:YES] 将开关设置为开启状态。但它不起作用。这是我所做的,

界面中

@property (weak, nonatomic) IBOutlet UISwitch *switchAutoPlay;

实施中

[[self switchAutoPlay] setOn:YES];

让我知道一个方法来完成这项工作。

初步检查,您是否连接了 UISwitch

的插座

在你的viewdidload

 switchAutoPlay.isOn --> is for on 

!switchAutoPlay.isOn --> is for off

方法

[switchAutoPlay addTarget: self action: @selector(flip:) forControlEvents: UIControlEventValueChanged];

对于

的方法
 - (IBAction)flip:(id)sender{

if([sender isOn]){
    NSLog(@"Switch is ON");
} else{
    NSLog(@"Switch is OFF");
}

}

选择-2

UISwitch,在开发者 API 中看到,任务 setOn: animated: 应该可以解决问题。

- (void)setOn:(BOOL)on animated:(BOOL)animated

所以要在你的程序中设置开关,你可以使用:

Objective-C

[switchAutoPlay setOn:YES animated:YES];