Swift将数据从子视图发送到父视图控制器

时间:2015-05-25 04:51:44

标签: ios swift

我正在开发一个应用程序,我有一个视图控制器和子视图。在子视图中我正在加载谷歌地图,在主视图上我有一个标签。

我的问题是如何将子视图(地图地理位置)中的数据传递到主视图上的标签,并在使用Swift更新位置时更新。

我发现的所有教程都使用prepareForSegue,我想在主视图上自动更新标签。

由于

更新:我似乎无法让委托方法起作用。代码如下。

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);
}
}

3 个答案:

答案 0 :(得分:5)

代表团模式是你的朋友。

在子视图控制器上声明父视图控制器将实现的协议。无论何时,只需在子视图控制器中保存的委托引用上调用方法 - 这将使用来自子VC的数据更新父视图控制器。

这个SO问题巧妙地解释了儿童父母代表团How do I set up a simple delegate to communicate between two view controllers?

答案 1 :(得分:4)

我在这里看到了一些答案,因此您可以在代码中更改某些行来解决问题,请查看:

protocol ChildViewControllerDelegate{
    func delegateMethod(childViewController:MapChildController, text:String)
}

我在没有childViewController传递方法的情况下使用,那么你可以将你想发送的任何内容传递给另一个视图,只是说明你要发送什么类型的数据,以防它是一个字符串。因此,您可以将ChildViewControllerDelegate更改为:

protocol ChildViewControllerDelegate{
    func delegateMethod(text:String)
}

你需要在班级MapController中做同样的事情:

func delegateMethod(text: String) {
    println("The text is " +  text);
}

您可以在How to send data back by popViewControllerAnimated for Swift

中看到更好的解释

答案 2 :(得分:0)

\n

除此之外,您还需要执行此操作以使委托工作:

var delegate:ChildViewControllerDelegate?