【问题标题】:Swift sending data from child view to parent view controllerSwift 将数据从子视图发送到父视图控制器
【发布时间】:2015-08-06 12:38:00
【问题描述】:

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

我的问题是如何将数据从子视图(地图地理位置)传递到主视图上的标签,并在使用 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);
}
}

【问题讨论】:

  • 你的标题和你的文字不匹配。你有谷歌地图的子视图,还是另一个视图控制器?如果是后者,您是否将其添加为主视图控制器的子视图控制器?
  • 谢谢。我已经更新了标题。我想将地理位置从子视图(具有 Google 地图)发送到父视图(具有标签)。
  • 使用委托。使主视图控制器成为视图的委托,并在需要时为其委托提供视图调用方法(在委托协议中定义)。
  • 好的,很好。我是 Swift 开发的新手,所以我会四处寻找,看看我能想出什么。

标签: ios swift


【解决方案1】:

委托模式是你的朋友。

在您的子视图控制器上声明父视图控制器将实现的协议。无论何时,都可以在子视图控制器中持有的委托引用上调用一个方法 - 这将使用来自子 VC 的数据更新父视图控制器。

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

【讨论】:

    【解决方案2】:
    var delegate:ChildViewControllerDelegate?
    

    除此之外,您还需要这样做才能使委托工作:

    delegate = MapController()
    

    【讨论】:

      【解决方案3】:

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

      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看到更好的解释

      【讨论】:

        猜你喜欢
        • 2018-08-06
        • 1970-01-01
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        相关资源
        最近更新 更多