迅速如何等待值改变

时间:2018-08-26 04:03:36

标签: ios swift xcode

我正在编写一个应用程序,用于测试您使用古典希腊语的能力。我有一些视图控制器。在测试视图控制器中,我有一个巨大的“开始”按钮,按下该按钮会启动一系列代码,如下所示:

@IBAction func test(_ sender: Any) {
        beginBtn.isHidden = true
        beginBtn.isEnabled = false
        answerOne.isHidden = false
        answerTwo.isHidden = false
        answerThree.isHidden = false
        answerFour.isHidden = false
        data.currentNumOQue = (data.qCQ + data.qWQ + data.qSQ)
    if data.chooseCAlertDataLoaded == false {
        data.chooseCharacterQuestionType.addAction(data.chooseCharacterQuestionTypeEngGrk)
        data.chooseCharacterQuestionType.addAction(data.chooseCharacterQuestionTypeGrkEng)
        data.chooseCAlertDataLoaded = true
    } else {
        print("not first question")
    }
    while data.currentQC <= data.qCQ {
        present(data.chooseCharacterQuestionType, animated: false, completion: nil)
        DispatchQueue.global(qos: .background).async {
            while data.chooseCAlertReturnsEngGrk == nil {

            }
            DispatchQueue.main.sync {
                if data.chooseCAlertReturnsEngGrk == true {
                    //Eng-Grk QUestion
                    data.chooseCAlertReturnsEngGrk = nil
                } else {
                    //Grk=Eng QUestion
                    data.chooseCAlertReturnsEngGrk = nil
                }
            }
        }
        data.currentQC += 1
        data.currentQ += 1
    }
    data.currentQC = 1
    data.currentQW = 1
    data.currentQS = 1

}

有人可以帮助我如何等到selectCAlertReturnsEngGrk值不为nil并执行时,但这样做时不会“冻结” UI吗?

1 个答案:

答案 0 :(得分:2)

重写didSet来调用某个函数..然后在该回调函数中,对参数执行任意操作。

class DataSomething {
    var chooseCAlertReturnsEngGrkObserver: ((_ newValue: SomeTime?) -> Void)?

    var chooseCAlertReturnsEngGrk: SomeType? = nil {
        didSet {
            chooseCAlertReturnsEngGrkObserver?(chooseCAlertReturnsEngGrk)
        }
    }
}