David Seek David Seek - 7 months ago 9
Swift Question

Swift / MotionKit.framework / how to update label

I'm trying to exploit motion data. I've created a test app using the MotionKit Framework.

print(testString)
prints the
x
variables correctly to the console in an one second interval. But my testLabel will not be updated once.

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


What am I missing? Help is very appreciated.

Answer

Just wrap your textfield text code in dispatch_async

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