Bra*_*dts 2 function uiswitch swift swift2
我在我的 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")
}
}
Run Code Online (Sandbox Code Playgroud)
调用该函数时,它会打印出我要求的所有内容,但开关值永远不会改变!
我尝试switch0.setOn(true, animated:true)在 ViewDidLoad 中使用,它在那里工作。当被这个函数调用时,它不起作用。如果我通过 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)
}
}
Run Code Online (Sandbox Code Playgroud)