iOS - 如何以编程方式设置UISwitch

问题描述:

我想以编程方式将我的UISwitch设置为开启或关闭。我该怎么办?我是iOS新手。

I want to set my UISwitch to on or off programmatically. How would I do that? I am an iOS newbie.

我不熟悉iOS中的复选框,但如果您使用的是UISwitch,然后在开发人员API中看到,任务 setOn:animated:应该可以解决问题。

I'm not familiar with a 'checkbox' in iOS, but if you are using a UISwitch, then as seen in the developer API, the task setOn: animated: should do the trick.

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

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

So to set the switch ON in your program, you would use:

Objective-C

[switchName setOn:YES animated:YES];

Swift

switchName.setOn(true, animated: true)