为什么我的UILabel在queryPedometerDataFromDate中没有被更改?

时间:2016-01-28 05:34:05

标签: ios swift uilabel

有谁知道为什么我不能让我的UILabel在queryPedometerDataFromDate中更改,即使它会在它之外更新?我在运行查询后尝试将stepCount发布到标签上。起初我以为这是一个NSNumber到String转换问题,但是两个print语句都向控制台打印相同的值。任何指导将不胜感激。我对这些东西很陌生。

var pedometer = CMPedometer()

    @IBOutlet weak var stepsLabel: UILabel!
    @IBAction func queryStepCount(sender: AnyObject) {

        if CMPedometer.isStepCountingAvailable() {

            let now = NSDate()
            let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
            let midnightOfToday = calendar!.startOfDayForDate(now)
            var stepCount = NSNumber(integer: 0)

            self.pedometer.queryPedometerDataFromDate(midnightOfToday, toDate: now, withHandler: { (CMPedometerData, error) -> Void in

                stepCount = CMPedometerData!.numberOfSteps
                let convertedStepCount = stepCount.stringValue
                print("Converted Step Count:" + convertedStepCount)
                print (stepCount)

                // Why doesn't this change my label text?
                self.stepsLabel.text = "CHANGED"

            })

            // But this does...
            self.stepsLabel.text = "CHANGED"

        }

1 个答案:

答案 0 :(得分:0)

如果未在主线程上执行查询,则行private void FixedUpdate() { ... U.Events.OnPlayerDisconnected += (UnturnedPlayer player) => { }; } public int indexHolder(int i) { return i; } public void playerTimeVars(object source, ElapsedEventArgs e) { int localIndex = indexHolder(index); playerTimer[localIndex] = 0; } 将不会更新UI。试试这个......

self.stepsLabel.text = "CHANGED"