簡體   English   中英

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")
    }
}

調用該函數時,它會打印出我要求的所有內容,但是開關值永遠不會改變!

我嘗試在switch0.setOn(true, animated:true)使用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接線到筆尖的正確插座上嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM