Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift/MotionKit.framework/如何更新标签_Ios_Swift_Uilabel - Fatal编程技术网

Ios Swift/MotionKit.framework/如何更新标签

Ios Swift/MotionKit.framework/如何更新标签,ios,swift,uilabel,Ios,Swift,Uilabel,我试图利用运动数据。我已经使用创建了一个测试应用程序print(testString)以1秒的间隔将x变量正确地打印到控制台。但是我的testLabel不会更新一次 import UIKit import MotionKit class ViewController: UIViewController { @IBOutlet weak var testLabel: UILabel! let motionKit = MotionKit() override func viewDidLoad

我试图利用运动数据。我已经使用创建了一个测试应用程序
print(testString)
以1秒的间隔将
x
变量正确地打印到控制台。但是我的testLabel不会更新一次

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

我错过了什么?非常感谢您的帮助。

只需将您的文本字段文本代码包装为dispatch\u async即可

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

只需将textfield文本代码包装在dispatch\u async中

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

非常感谢。成功了。你能解释一下为什么吗?所以我不只是复制/粘贴,而是理解它,请^^所有UI更新都应该在主线程上完成。motionKit.getAccelerometerValues在后台线程上运行,因此当您更新该线程上的标签时,它不会更新到UI,因此要更新主线程上的标签,您必须将其包装在dispatch_async(dispatch_get_main_queue()中)…在主线程上执行。谢谢。就是这样。你能解释一下原因吗?所以我不只是复制/粘贴,而是理解它,请^^所有UI更新都应该在主线程上完成。你的motionKit.getAccelerometerValues在后台线程上运行,所以当你在该线程上更新标签时,它不会更新到UI,所以要在m上更新标签ain线程您必须将其包装到在主线程上执行的dispatch\u async(dispatch\u get\u main\u queue()…中。