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

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

最佳答案

如果您使用的是 UISwitch,那么正如在开发人员 API 中看到的那样,任务 setOn: animated: 应该可以解决问题。

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

因此,要在程序中将开关设置为 ON,您可以使用:

Objective-C

[switchName setOn:YES animated:YES];

swift

switchName.setOn(true, animated: true)

https://stackoverflow.com/questions/7799760/

相关文章:

ios - 带有透明孔的 CALayer

ios - 在单行 UILabel 旁边居中 NSTextAttachment 图像

objective-c - iOS8 - 约束含糊地暗示高度为零

ios - "From View Controller"使用 UIViewControllerCon

objective-c - iOS 7.0.3 上的 "HelveticaNeue-Italic"发

ios - UICollectionViewCell 上的长按手势

iphone - 关闭呈现的 View Controller

ios - 设置 UILabel 行距

ios - 检查 AVPlayer 的播放状态

ios - 对 UITableView 使用 didSelectRowAtIndexPath 或 p