gpt4 book ai didi

swift - UISwitch 不会改变 Swift 中的值

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:27 24 4
gpt4 key购买 nike

我在我的 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)
}
}

关于swift - UISwitch 不会改变 Swift 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35802017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com