Wayne Wayne - 3 months ago 7x
Swift Question

I am really struggling to transfer data backwards to a UIView Controller

I have several view controllers within a split view. With the one, clientViewController I have two text fields - "latitude" & "longitude"

I move onto a view controller that runs a map view etc. I post the latitude and longitude of the current location. In this I have a protocol and I have used the following code which seems to work fine:

protocol MappingViewControllerDelegate {
func transferData(latitudeTfr: String!, longitudeTfr: String!)

class MappingViewController: UIViewController, CLLocationManagerDelegate, `MKMapViewDelegate, UITextFieldDelegate {`

var delegate: MappingViewControllerDelegate?
var latitudeTfr: String!
var longitudeTfr: String!

override func viewWillDisappear(animated: Bool) {
latitudeTfr = latitudeLabel.text!
longitudeTfr = longitudeLabel.text!
self.delegate?.transferData(latitudeTfr!, longitudeTfr: longitudeTfr!)
print("Longitude is: \(longitudeTfr)")
print("Latitude is: \(latitudeTfr)")


Back in the clientViewController I have the following but it does not seem to be fetching / transferring the data:

class ClientViewController: UIViewController, UITextFieldDelegate, MappingViewControllerDelegate {

func transferData(latitudeTfr: String!, longitudeTfr: String!) {
self.latitudeText.text = "\(latitudeTfr!)"
self.longitudeText.text = "\(longitudeTfr!)"

Somehere in the clientViewController I am missing something and cannot figure out what I am doing wrong. Any help would be much appreciated.

Kind regards



You need to assign your protocol delegate to the viewcontroller that you're conforming.


let vc = self.storyboard!.instantiateViewControllerWithIdentifier("clientView") as! ClientViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)