2015-10-16 72 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];