0

I have managed to get this to print the Relative Altitude data to the log however what I would like to do is get it to print to a designated label.Any help would be appreciated.

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)")

                }
        })

    }

}

}
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • using `self.altitudeLabel.text` ? – Raptor Feb 27 '15 at 08:00
  • Welcome to StackOverflow if you are new iOS I think [this](https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/DesigningaUserInterface.html#//apple_ref/doc/uid/TP40011343-CH6-SW1) is greate tutorial :) – Huy Nghia Feb 27 '15 at 08:01
  • Thanks @Raptor That worked perfect. How would I change that to a Double Variable? Right now it outputs too many decimal points. – James Dunnigan Feb 27 '15 at 08:15
  • see the answer below. – Raptor Feb 27 '15 at 08:19

2 Answers2

0

You can use the following line to assign a float value with 2 decimal places to your UILabel outlet:

self.altitudeLabel.text = String(format: "%0.2f", data.relativeAltitude)
Raptor
  • 53,206
  • 45
  • 230
  • 366
0

I ended up using this and it will due for now:

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