当我将它们插入动作按钮时,为什么我的CLLocation功能会停止工作?

时间:2015-02-03 20:17:37

标签: ios xcode swift

所以我一直在使用本教程http://www.veasoftware.com/tutorials/2014/10/18/xcode-6-tutorial-ios-8-current-location-in-swift来学习如何使用CLLocation类获取一些基本的位置信息。当我在viewDidLoad中声明委托和其他方法时,我得到它成功运行。好极了!

import UIKit
import CoreLocation

class ProfileScreenViewController: UIViewController, CLLocationManagerDelegate    {

let locationManager = CLLocationManager()

@IBOutlet var userInfoLabel: UILabel!
var currentUser = PFUser.currentUser()!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated:true);

        }




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


@IBAction func testUserInfoButton(sender: UIButton) {
    PFUser.logOut()
    var currentUser = PFUser.currentUser()
    self.navigationItem.setHidesBackButton(true, animated:true);
    self.navigationController?.popToRootViewControllerAnimated(true)
}

@IBAction func getLocationButton(sender: UIButton) {
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("error " + error.localizedDescription)
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

            if error != nil {
                println("error" + error.localizedDescription)
            }

            if placemarks.count > 0 {
                let pm = placemarks[0] as CLPlacemark
                self.displayLocationInfo(pm)
            } else {
                println("Error " + error.localizedDescription)
            }
        })
    }

    func displayLocationInfo(placemark: CLPlacemark){
        locationManager.stopUpdatingLocation()
        println(placemark.locality)
        println(placemark.postalCode)
        println(placemark.administrativeArea)
        println(placemark.country)
    }



}

http://imgur.com/a/pI1tP

然而!当我尝试将所有内容移动到按钮动作时,一切都崩溃了。代码是一样的,我不知道如何解决这个问题。我试着一遍又一遍地重新打字。错误一直告诉我“定义与先前值的冲突”,或者这样的视图控制器没有成员名称x。我甚至去了另一个教程,在另一个网站上,他基本上写了相同的代码但是按钮(就像我正在尝试做的那样)。但他们让它发挥作用。

在摆弄之后,我把它归结为3个错误。但令人沮丧的是,它仍然是我以前工作过的完全相同的代码...当我在一个按钮内传输代码时,我觉得我在这里遗漏了一些东西,但我对Swift / Xcode还是有点新鲜。

根据要求,工作代码,没有错误:

import UIKit
import CoreLocation

class ProfileScreenViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

@IBOutlet var userInfoLabel: UILabel!
var currentUser = PFUser.currentUser()!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated:true);

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("error " + error.localizedDescription)
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

        if error != nil {
            println("error" + error.localizedDescription)
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
        } else {
            println("Error " + error.localizedDescription)
        }
    })
}

func displayLocationInfo(placemark: CLPlacemark){
        locationManager.stopUpdatingLocation()
        println(placemark.locality)
        println(placemark.postalCode)
        println(placemark.administrativeArea)
        println(placemark.country)
}

无论如何,如果您可以查看代码,或者遇到类似的问题,我感谢您。这是一件小事,但4个小时后,我不得不寻求帮助。

抱歉,我不得不链接我的图片... Stackoverflow没有让我发布图片......非常无益......

2 个答案:

答案 0 :(得分:0)

糟糕。显然,你无法在动作中定义函数......奇怪的是,我已经在我的代码中的其他地方完成了它。

import UIKit
import CoreLocation

class ProfileScreenViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

@IBOutlet var userInfoLabel: UILabel!
var currentUser = PFUser.currentUser()!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated:true);

        }

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("error " + error.localizedDescription)
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

        if error != nil {
            println("error" + error.localizedDescription)
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
        } else {
            println("Error " + error.localizedDescription)
        }
    })
}

func displayLocationInfo(placemark: CLPlacemark){
        locationManager.stopUpdatingLocation()
        println(placemark.locality)
        println(placemark.postalCode)
        println(placemark.administrativeArea)
        println(placemark.country)
}




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


@IBAction func testUserInfoButton(sender: UIButton) {
    PFUser.logOut()
    var currentUser = PFUser.currentUser()
    self.navigationItem.setHidesBackButton(true, animated:true);
    self.navigationController?.popToRootViewControllerAnimated(true)
}

@IBAction func getLocationButton(sender: UIButton) {
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()

}

答案 1 :(得分:0)

使用Swift 2时遇到类似问题,我不得不改变: -

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in

可以阻止您的一个错误。