![](/img/trans.png)
[英]Passing Data From Parent View Controller to Child View Controller Swift 4
[英]Swift sending data from child view to parent view controller
我正在開發一個應用程序,其中有一個視圖控制器和子視圖。 在子視圖上我正在加載谷歌地圖,在主視圖上我有一個標簽。
我的問題是如何將數據從子視圖(地圖地理位置)傳遞到主視圖上的標簽,並在使用 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);
}
}
委托模式是你的朋友。
在您的子視圖控制器上聲明父視圖控制器將實現的協議。 無論何時,都可以在子視圖控制器中持有的委托引用上調用一個方法——這將使用來自子 VC 的數據更新父視圖控制器。
這個 SO 問題巧妙地解釋了子父委托如何設置一個簡單的委托來在兩個視圖控制器之間進行通信?
我在這里看到了一些答案,因此您可以通過更改代碼中的某些行來解決您的問題,請查看:
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
var delegate:ChildViewControllerDelegate?
除此之外,您還需要這樣做才能使委托工作:
delegate = MapController()
self.navigationController?.popViewController(animated: true)
let vc = self.navigationController?.viewControllers.last as! MainViewController
vc.textfield.text = "test"
如果你願意,你可以做,也許你不需要這個答案,但其他用戶有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.