gpt4 book ai didi

ios - Swift 将数据从 subview 发送到父 View Controller

转载 作者:搜寻专家 更新时间:2023-10-30 22:08:59 25 4
gpt4 key购买 nike

我正在开发一个有 View Controller 和 subview 的应用程序。在 subview 上,我正在加载 Google map ,在主视图上,我有一个标签。

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

我找到的所有教程都使用 prepareForSegue,我想在其中自动更新标签,因为它在主视图上。

谢谢

更新:我似乎无法让委托(delegate)方法工作。代码如下。

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

最佳答案

委托(delegate)模式是你的 friend 。

在父 View Controller 将实现的 subview Controller 上声明一个协议(protocol)。无论何时,都可以在 subview Controller 中的委托(delegate)引用上调用一个方法 - 这将使用子 VC 中的数据更新父 View Controller 。

这个 SO 问题巧妙地解释了子父委托(delegate) How do I set up a simple delegate to communicate between two view controllers?

关于ios - Swift 将数据从 subview 发送到父 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com