2015-05-25 23 views
5

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

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

+0

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

+0

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

Trả lời

4

Phái đoàn mô hình là bạn của bạn.

Khai báo giao thức trên bộ điều khiển xem con bạn mà trình điều khiển chế độ xem gốc sẽ triển khai. Bất cứ khi nào bạn muốn, hãy gọi một phương thức trên tham chiếu ủy nhiệm bạn đang giữ trong bộ điều khiển xem con - điều này sẽ cập nhật bộ điều khiển xem cha với dữ liệu từ VC con.

này SO câu hỏi giải thích đoàn mẹ con gọn gàng How do I set up a simple delegate to communicate between two view controllers?

0
var delegate:ChildViewControllerDelegate? 

Thêm vào đó, bạn cũng cần phải làm điều này để làm cho công tác đại biểu:

delegate = MapController() 
3

tôi thấy một số câu trả lời ở đây , để bạn có thể giải quyết vấn đề của mình khi thay đổi một số dòng trong mã của mình, hãy xem:

protocol ChildViewControllerDelegate{ 
    func delegateMethod(childViewController:MapChildController, text:String) 
} 

Tôi sử dụng mà không cần childViewController truyền theo phương thức thì bạn chỉ có thể chuyển bất kỳ thứ gì bạn muốn gửi đến chế độ xem khác chỉ cần nói loại dữ liệu bạn muốn gửi, trong trường hợp đó là một Chuỗi. Vì vậy, bạn có thể thay đổi ChildViewControllerDelegate tới:

protocol ChildViewControllerDelegate{ 
    func delegateMethod(text:String) 
} 

Bạn cần làm điều tương tự trong lớp MapController:

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

Bạn có thể thấy một lời giải thích tốt hơn trong: How to send data back by popViewControllerAnimated for Swift

Các vấn đề liên quan