我收到此错误/警告,但没有找到任何答案。问题是我有两个代码文件: ViewController.swift 和 CoreLocationDelegate.swift 。
在下面的代码(CoreLocationDelegate.swift)中,您将找到 showLocationServicesDeniedAlert()方法。在 getLocation()中调用了哪个。
import UIKit
import CoreLocation
class CoreLocationDelegate: UIViewController, CLLocationManagerDelegate {
// MARK: - CLLocationManagerDelegate
let locationManager = CLLocationManager()
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("didFailWithError: \(error)")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
print("didUpdateLocations: \(newLocation)")
}
// MARK: getLocation method
func getLocation() {
// Check for current authorisation status of app:
let authStatus = CLLocationManager.authorizationStatus()
if authStatus == .NotDetermined {
locationManager.requestAlwaysAuthorization()
return
}
if authStatus == .Denied || authStatus == .Restricted {
showLocationServicesDeniedAlert()
}
// Get current location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.startUpdatingLocation()
}
func showLocationServicesDeniedAlert() {
let alert = UIAlertController(title: "Location Services Disabled", message: "Please enable location services for this app in Settings.", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(okAction)
presentViewController(alert, animated: true, completion: nil)
}
}
现在,在以下代码(ViewController.swift)中的按钮内使用getLocation()方法会出现问题:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBAction func getLocationButton(sender: AnyObject) {
CoreLocationDelegate().getLocation()
}
}
按下按钮时(在模拟器中)我得到警告:尝试在视图不在窗口层次结构中显示!
知道我缺少什么或需要掌握什么?