所以我有一个主视图,它只包含一个tableview,然后是一个名为RouteViewController
的第二个视图控制器,它由mapView
和UITextView
组成。我得到用户的当前位置,如果位置管理器失败,那么我会向他们显示有关它的警报。容易吗?
但是每次调用警报视图控制器时,我都会首先收到以下警告:
警告:尝试显示UIAlertController:0x7cb58200 “AppName”.RouteViewController:0x7e4450e0,同时演示文稿在 进步!
警报确实出现了,我可以取消它和一切。然后当我回到我的桌面视图并点击另一个项目时,我这次收到以下警告:
尝试加载视图控制器的视图 不允许取消分配,可能导致未定义的行为 (UIAlertController:0x7cb58200)
就像之前一样,警报出现,我甚至可以取消它。
我已经找到了解决方案,但到目前为止还没有任何实用方法。
PS:我保留locations.count > 100
以便在位置管理员失败时尝试模拟方案
提前感谢您的任何帮助。
// RouteViewController.swift
import UIKit
import MapKit
import CoreLocation
class RouteViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var routeDescriptionTextView: UITextView!
@IBOutlet weak var mapView: MKMapView!
var route: Routes?
let locationManager = CLLocationManager()
func updateRouteView()
{
let routeName = "\((route?.routeName)!)"
let routeNumber = "\((route?.routeNumber)!)"
let routeDescription = ((route?.routeDescription)! as NSArray).componentsJoinedByString("\n")
let routeTimes = "\((route?.routeTimes)!)"
let routeDaysOfOperation = ((route?.routeDaysOfOperation)! as NSArray).componentsJoinedByString(", ")
routeDescriptionTextView.text = "Route Name: \(routeName) \n\n Route Number: \(routeNumber) \n\n \(routeDescription) \n\n \(routeDaysOfOperation) \n\n \(routeTimes)"
}
func showAlertToUser(errorTitle: String, errorMessage: String)
{
let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
updateRouteView()
title = "\((route?.routeName)!)"
routeDescriptionTextView.contentInset = UIEdgeInsetsMake(1.0, 0.0, 0.0, 0.0) // this is for the textview scrolling size
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization() // change to always if background needed
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//TODO: Make this better. I don't like the way it is at the moment.
//TODO: Make location tracker work when app is in the background
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if (locations.count > 100)
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
}
//TODO: Add an alert to the user for the locationManagerError
//TODO: Okay, the above TODO is partially complete.Fix the warnings received from view presentation
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
showAlertToUser("Location Manager Error", errorMessage: error.localizedDescription)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}