Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速将数据从子视图发送到父视图控制器_Ios_Swift - Fatal编程技术网

Ios 快速将数据从子视图发送到父视图控制器

Ios 快速将数据从子视图发送到父视图控制器,ios,swift,Ios,Swift,我正在开发一个应用程序,我有一个视图控制器和子视图。在子视图中,我正在加载谷歌地图,在主视图中,我有一个标签 我的问题是如何将数据从子视图(地图地理位置)传递到主视图上的标签,并在使用Swift更新位置时进行更新 我找到的所有教程都使用prepareForSegue,我只想在主视图中自动更新标签 谢谢 更新:我似乎无法使委托方法工作。代码如下 斯威夫特地图 import UIKit protocol ChildViewControllerDelegate{ func delegateM

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

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

我找到的所有教程都使用prepareForSegue,我只想在主视图中自动更新标签

谢谢

更新:我似乎无法使委托方法工作。代码如下

斯威夫特地图

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

代表团模式是你的朋友

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

这个问题很好地解释了子-父委托关系

除此之外,您还需要执行以下操作以使代理正常工作:

delegate = MapController()

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

protocol ChildViewControllerDelegate{
    func delegateMethod(childViewController:MapChildController, text:String)
}
我使用的方法没有
childViewController
passingby方法,那么您可以将您想要发送的任何内容传递到另一个视图,只要说明您想要发送的数据类型,以防它是字符串。因此,您可以将
ChildViewControllerDelegate
更改为:

protocol ChildViewControllerDelegate{
    func delegateMethod(text:String)
}
您需要在类中执行相同的操作
MapController

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

您可以在中看到更好的解释:

您的标题和文本不匹配。你有谷歌地图的子视图,还是另一个视图控制器?如果是后者,是否已将其添加为主视图控制器的子视图控制器?谢谢。我已经更新了标题。我想将地理位置从子视图(有谷歌地图)发送到父视图(有标签)。使用委派。使主视图控制器成为视图的委托,并在需要时在其委托上具有视图调用方法(在委托协议中定义)。好的。我是快速发展的新手,所以我会四处看看,看看我能想出什么。
func delegateMethod(text: String) {
    println("The text is " +  text);
}