标签没有更新swift

时间:2015-03-24 00:19:33

标签: ios swift uilabel

我是iOS新手。我正在使用MotionKit从加速度计中检索数据。我可以在终端上看到加速计的值每0.1秒检索一次,遗憾的是标签只更新了一次。为什么标签没有更新?

@IBOutlet weak var xLabel: UILabel!
@IBOutlet weak var yLabel: UILabel!
@IBOutlet weak var zLabel: UILabel!

var xAccel = 0.0
var yAccel = 0.0
var zAccel = 0.0


private let queue = NSOperationQueue()
let motionKit = MotionKit()




override func viewDidLoad() {
    super.viewDidLoad()

    motionKit.getAccelerometerValues(interval: 0.1){
        (x, y, z) in

        println("x: \(x)")
        println("y:  \(y)")
        println("z: \(z)")
        println();


        self.xAccel = x
        self.yAccel = y
        self.zAccel = z





        self.xLabel.text = "\(self.xAccel)"

        self.yLabel.text = "\(self.yAccel)"

        self.zLabel.text = "\(self.zAccel)"








    }

1 个答案:

答案 0 :(得分:3)

由于此MotionKit框架作为块执行,您必须更新主队列中的UILabel,这样的事情将解决此问题,但仍然真的很乱

import UIKit


class ViewController: UIViewController {
    @IBOutlet weak var xLabel: UILabel!
    @IBOutlet weak var yLabel: UILabel!
    @IBOutlet weak var zLabel: UILabel!

    var xAccel = 0.0
    var yAccel = 0.0
    var zAccel = 0.0


    private let queue = NSOperationQueue()
    let motionKit = MotionKit()

    override func viewDidLoad() {
        super.viewDidLoad()
        motionKit.getAccelerometerValues(interval: 0.1){
            (x, y, z) in

            println("x: \(x)")
            println("y:  \(y)")
            println("z: \(z)")
            println();


            self.xAccel = x
            self.yAccel = y
            self.zAccel = z

            dispatch_async(dispatch_get_main_queue(), {
                self.xLabel.text = "\(self.xAccel)"
                self.yLabel.text = "\(self.yAccel)"
                self.zLabel.text = "\(self.zAccel)"

            });

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

希望这有帮助

相关问题