从子视图向父视图控制器发送数据的Swift方法

6
我正在开发一个应用程序,其中包含一个视图控制器和子视图。在子视图上,我加载了Google地图,在主视图上有一个标签。
我的问题是如何将数据从子视图(地图地理位置)传递到主视图上的标签,并使用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);
}
}

2
您的标题和文本不匹配。您是否有一个带有Google地图的子视图,还是另一个视图控制器?如果是后者,您是否已将其添加为主视图控制器的子视图控制器? - rdelmar
谢谢。我已经更新了标题。我想要从子视图(带有谷歌地图)发送地理位置到父视图(带有标签)。 - puks1978
使用委托。将主视图控制器作为视图的代理,并在视图需要时调用其代理中定义的方法(通过委托协议定义)。 - rdelmar
好的,我刚开始学习Swift开发,所以我会找找看并尝试一下。 - puks1978
4个回答

7

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

您可以在此链接中查看更好的解释:如何使用popViewControllerAnimated将数据发送回Swift


1
 self.navigationController?.popViewController(animated: true)
        let vc = self.navigationController?.viewControllers.last as! MainViewController
        vc.textfield.text = "test"

如果你愿意,你可以这样做。也许你不需要这个答案,但其他用户可能会觉得有用。


0
var delegate:ChildViewControllerDelegate?

此外,您还需要执行以下操作才能使委托起作用:
delegate = MapController()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接