Ich entwickle eine App, wo ich einen View-Controller und Subview habe. In der Unteransicht lade ich Google Maps und in der Hauptansicht habe ich ein Label.Swift Senden von Daten von der Kind-Ansicht zum übergeordneten View-Controller
Meine Frage ist, wie kann ich Daten aus der Unteransicht (Geo-Location) an die Bezeichnung in der Hauptansicht übergeben und diese aktualisiert haben, wenn der Standort mit Swift aktualisiert wird.
Alle Tutorials, die ich gefunden habe, verwenden prepareForSegue, wo ich das Label automatisch aktualisieren möchte, wie es in der Hauptansicht ist.
Dank
aktualisiert: Ich kann nicht scheinen, die delegierte Methode zur Arbeit zu kommen. Code unten.
MapChildController.swift
import UIKit
protocol ChildViewControllerDelegate{
func delegateMethod(childViewController:MapChildController, text:String)
}
class MapChildController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
var didFindMyLocation = false
var myLocations: [CLLocation] = []
var delegate:ChildViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 15, bearing: 0, viewingAngle: 45)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if NSProcessInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion(majorVersion: 8, minorVersion: 0, patchVersion: 0)) {
println("iOS >= 8.0.0")
locationManager.requestWhenInUseAuthorization()
//locationManager.requestAlwaysAuthorization()
}
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
locationManager.startUpdatingLocation()
//locationManager.startMonitoringSignificantLocationChanges()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
var mapView = self.view as! GMSMapView
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 15.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {
// println("Last location: \(locations.last)")
self.delegate?.delegateMethod(self, text: "from child")
}
}
MapController.swift
import Foundation
import UIKit
class MapController : UIViewController, ChildViewControllerDelegate
{
@IBOutlet weak var Label: UILabel!
var LabelText = String()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func delegateMethod(controller: MapChildController, text: String) {
println("The text is " + text);
}
}
Ihr Titel und Ihren Text don nicht übereinstimmen. Haben Sie eine Unteransicht mit Google Maps oder ist das ein anderer View-Controller? Wenn letzteres, hast du es als Kind View-Controller von Ihrem Haupt-View-Controller hinzugefügt? – rdelmar
Danke. Ich habe den Titel aktualisiert. Ich möchte Geo-Standorte von der Kinderansicht (mit Google-Karte) zur Elternansicht senden (die das Label hat). – puks1978
Delegierung verwenden Machen Sie den Hauptansichtscontroller, den Delegaten der Sicht, und weisen Sie die Aufrufmethode (in einem Delegatprotokoll definiert) für den Delegaten auf, wenn dies erforderlich ist. – rdelmar