UISwitch 未更改 Swift 中的值
UISwitch not changing value in Swift
我在 Swift 2.2 应用程序中使用这个小功能:
func changeSwitchValue() {
if self.lampValue == 1 {
self.switch0.setOn(true, animated: false)
print("changed to 1")
}
if self.lampValue == 0 {
self.switch0.setOn(false, animated: true)
print("changed to 0")
}
}
调用该函数时,它会打印出我要求的所有内容,但开关值永远不会改变!
我尝试在 ViewDidLoad 中使用 switch0.setOn(true, animated:true)
,它在那里工作。当被这个函数调用时,它不起作用。如果我通过 IBAction 调用这段代码,它也不起作用..
有人知道这个奇怪的问题吗?有什么建议吗?
谢谢!
确保在主线程上执行 ui 操作
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { () -> Void in
// do the networking here
// if you are done update the ui on the main thread
dispatch_async(dispatch_get_main_queue()
) { () -> Void in
self.mySwitch.setOn(!self.mySwitch.on, animated: true)
}
}
您没有发布足够的代码,但我假设 switch0 是一个位于 nib 中的 UISwitch,并且在视图控制器中声明如下:
@IBOutlet weak var switch0: UISwitch!
如果不是这种情况,则所有赌注都将取消 :) 所以我的反馈是,您确定 switch0 连接到笔尖中的正确插座吗?
我在 Swift 2.2 应用程序中使用这个小功能:
func changeSwitchValue() {
if self.lampValue == 1 {
self.switch0.setOn(true, animated: false)
print("changed to 1")
}
if self.lampValue == 0 {
self.switch0.setOn(false, animated: true)
print("changed to 0")
}
}
调用该函数时,它会打印出我要求的所有内容,但开关值永远不会改变!
我尝试在 ViewDidLoad 中使用 switch0.setOn(true, animated:true)
,它在那里工作。当被这个函数调用时,它不起作用。如果我通过 IBAction 调用这段代码,它也不起作用..
有人知道这个奇怪的问题吗?有什么建议吗?
谢谢!
确保在主线程上执行 ui 操作
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { () -> Void in
// do the networking here
// if you are done update the ui on the main thread
dispatch_async(dispatch_get_main_queue()
) { () -> Void in
self.mySwitch.setOn(!self.mySwitch.on, animated: true)
}
}
您没有发布足够的代码,但我假设 switch0 是一个位于 nib 中的 UISwitch,并且在视图控制器中声明如下:
@IBOutlet weak var switch0: UISwitch!
如果不是这种情况,则所有赌注都将取消 :) 所以我的反馈是,您确定 switch0 连接到笔尖中的正确插座吗?