2015-10-16 19 views
-1

我试图使用[self.switchAutoPlay setOn:YES]将开关设置为开启状态。但它不起作用。在这里我做了什么,如何设置iOS开关的开关状态?

在接口

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

在实施

[[self switchAutoPlay] setOn:YES]; 

让我知道一种方法,使这项工作。

+0

你连接你的出路何在? – Kreiri

+0

你是否正确地将IBOutlet挂钩? 'self.switchAutoPlay.on = YES;'应该工作。同时检查self.switchAutoPlay不是'nil' – Flexicoder

回答

1

初步检查,你连你的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];