Swift - 如何更新iOS中相对高度变化的标签

时间:2015-02-27 07:55:02

标签: ios swift

我已设法将相关海拔高度数据打印到日志中,但我想要做的是将其打印到指定的标签。非常感谢。

import UIKit
import CoreMotion

class ViewController: UIViewController {


    @IBOutlet weak var altitudeLabel: UILabel!
    let altimeter = CMAltimeter()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    @IBAction func start(sender: AnyObject) {

        if CMAltimeter.isRelativeAltitudeAvailable() {
            altimeter.startRelativeAltitudeUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { data, error in
                if (error == nil) {
                    println("relative Altitude: \(data.relativeAltitude)")
                    println("Pressure: \(data.pressure)")

                }
        })

    }

}

}

2 个答案:

答案 0 :(得分:0)

您可以使用以下行为UILabel插座指定一个带有2个小数位的浮点值:

self.altitudeLabel.text = String(format: "%0.2f", data.relativeAltitude)

答案 1 :(得分:0)

我最终使用了它,它现在将到期:

     var myIntValue:Int = Int(data.relativeAltitude)
                println ("My value is \(myIntValue)")
                self.altitudeLabel.text = ("\(myIntValue) M")