ios - Swift 中带有 UISwitch 状态的 If 语句

标签 ios swift

我想知道如何为开关状态编写 if 语句。我试过这个:

if switch1 == true{
   label.text = "Light gray"
   scrollView.backgroundColor = UIColor.lightGrayColor()
}

在此之前我创建了这个网点:

@IBOutlet weak var switch1: UISwitch!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var label: UILabel!

但这行不通。我该怎么做?

最佳答案

使用 .on 属性。您也不需要以这种方式与 true 进行比较:

if switch1.on {
   label.text = "Light gray"
   scrollView.backgroundColor = UIColor.lightGrayColor()
}

关于ios - Swift 中带有 UISwitch 状态的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251274/

相关文章:

ios - SwiftUI 列表中某些行的布局问题

ios - 优化 iOS 应用中关键代码的性能

ios - 创建主题标签字段以在 Swift 中进行解析

Swift 3.0 UserDefaults 获取值错误

ios - iOS:一个文本字段中的电子邮件ID和电话号码评估

ios - 属性(property)观察员及其行为

mysql - Vapor MySQL - 未显示为导入值

ios - !] 在 `Example_Framework` 中找不到 `.` 的 podspec

iphone - Xcode 5、iOS7 和 64 位与 32 位

ios - 根据方向切换 Storyboard