2016-04-25 15 views
0

我試圖利用運動數據。我使用MotionKit Framework創建了一個測試應用程序。 print(testString)以一秒爲間隔正確地將x變量打印到控制檯。但我的testLabel不會更新一次。Swift/MotionKit.framework /如何更新標籤

import UIKit 
import MotionKit 

class ViewController: UIViewController { 

@IBOutlet weak var testLabel: UILabel! 

let motionKit = MotionKit() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    motionKit.getAccelerometerValues(1.0) { (x, y, z) ->() in 
     let testString = String(x) 
     self.testLabel.text = testString 
     print(testString)    
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 
} 

我在想什麼?非常感謝幫助。

回答

1

只是包裝你的文本框的文本代碼dispatch_async

dispatch_async(dispatch_get_main_queue(), ^{ 
     self.testLabel.text = testString 
    }); 
+0

謝謝。它做到了。你能解釋我爲什麼嗎?所以我不只是複製/粘貼,但理解它,請^^ –

+0

所有UI更新應該在MainThread上完成。您的motionKit.getAccelerometerValues在後臺線程上運行,因此當您更新該線程上的標籤時,它不會更新爲UI,因此要更新主線程上的標籤,必須將其包裝在dispatch_async(dispatch_get_main_queue()主線程 –

+0

好吧謝謝你的努力:) –