Tôi đang phát triển một ứng dụng mà tôi có bộ điều khiển chế độ xem và chế độ xem phụ. Trên subview tôi đang tải bản đồ Google và trên giao diện chính, tôi có một nhãn.Hoán chuyển dữ liệu từ chế độ xem con đến bộ điều khiển chế độ xem cha mẹ
Câu hỏi của tôi là làm cách nào để chuyển dữ liệu từ chế độ xem con (vị trí địa lý bản đồ) đến nhãn trên giao diện chính và được cập nhật khi vị trí được cập nhật bằng Swift.
Tất cả các hướng dẫn tôi đã tìm thấy sử dụng preparForSegue nơi tôi muốn chỉ cập nhật nhãn tự động khi nó ở chế độ xem chính.
Cảm ơn
Đã cập nhật: Tôi có vẻ không làm cho phương thức ủy nhiệm hoạt động. Mã dưới đây.
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);
}
}
Tiêu đề của bạn và văn bản của bạn don không phù hợp. Bạn có chế độ xem phụ với Google Maps hay bộ điều khiển chế độ xem khác không? Nếu sau này, bạn đã thêm nó như là một bộ điều khiển xem con của bộ điều khiển xem chính của bạn? – rdelmar
Cảm ơn. Tôi đã cập nhật tiêu đề. Tôi muốn gửi vị trí địa lý từ chế độ xem con (có bản đồ Google) đến chế độ xem gốc (có nhãn). – puks1978
Sử dụng ủy quyền. Tạo bộ điều khiển xem chính, đại biểu của khung nhìn và có phương thức gọi dạng xem (được định nghĩa trong giao thức đại biểu) trên đại biểu của nó khi cần. – rdelmar