Pracuję nad moją pierwszą szybką aplikacją na iPada. Do tej pory mam podstawowy widok mapy z przyciskiem na dolnym pasku narzędzi, który chciałbym odświeżyć i skupić się na lokalizacji użytkowników po kliknięciu.Odświeżenie lokalizacji przycisku mapy
Obecnie mam ten kod:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var location: CLLocation!
let locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var refresh: UIBarButtonItem!
@IBAction func refresh(sender: AnyObject) {
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// location delegate methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
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()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error code: " + error.localizedDescription)
}
}
Jak mogę uzyskać przycisk Odśwież, aby to zrobić? Ja naprawdę potrzebuję pomocy ponieważ jestem nowy Swift/Xcode :)
Dziękuję
Nie jestem pewien, czy dobrze cię zrozumiałem. Dlaczego po prostu nie umieszczasz akcji w kontrolerze i nie odświeżasz danych w swoich usługach/menedżerach? – razor118
Jak mam to zrobić, przepraszam? Jestem naprawdę nowy w Xcode :(@ razor118 Chciałbym, aby przycisk odświeżania odświeżył ostatnią lokalizację po naciśnięciu – Oscar