简体   繁体   中英

Not able to pass value from one View Controller to another View Controller

I am learning Swift and doing one project where I want to send an array of CLLocationCoordinate2D from one view controller to another View Controller (Map View) so that I can draw multiple annotation at one time. If my array of CLLocationCoordinate2D has 5 sets of longitudes and latitudes, I want all of them to get passed to my MapViewController and there I want all the annotations to be drawn under in MapView at go without user interactions.

Problem: In my MapView all I see is empty output in console [] but I am passing a set of longitude and latitude from my initial view controller. How can I keep the values intact? `

Problem: Once I get all the coordinates in my Map2ViewController then I can simply loop through all the elements to draw the annotations as the map is loading in the MapView right?

Below is what I have tried until now.

My "SearchBloodViewController.swift" //This is my initial view which generates Longitude and Latitude.

import UIKit
import MapKit


class SearchBloodViewController: UIViewController, UITextFieldDelegate,  CLLocationManagerDelegate 
{
var array: [PFObject] = [PFObject]()
var arra: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
var lat_: Int = 0
var long_: Int = 0
@IBOutlet weak var bloodType: UITextField!
@IBAction func search(sender: UIButton)
{
    var query = PFQuery(className: "_User")
    query.whereKey("BloodGroup", equalTo: bloodType.text!)
    do
    {
        try array = query.findObjects() as [PFObject]
    }
    catch
    {

    }
    for arr in array
    {
        self.lat_ = arr["Latitude"] as! Int
        self.long_ = arr["Longitude"] as! Int
        var cor =  CLLocationCoordinate2D.init(latitude: Double(self.lat_), longitude: Double(self.long_))
        arra.append(cor)
        }
            print(arra)
    // The console Output of this is
  //  [C.CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0), 
//C.CLLocationCoordinate2D(latitude: 42.0, longitude: -75.0),
 //C.CLLocationCoordinate2D(latitude: 41.0, longitude: -87.0)]

     }
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
    if (segue.identifier == "drawmap")
    {
        let navigationController = segue.destinationViewController as! UINavigationController
        let controller = navigationController.topViewController as! Map2ViewController
      //  controller.delegate = self
        controller.ar = self.arra
       }


   }
  override func viewDidLoad()
   {
       super.viewDidLoad()
   }

My Map2ViewController: (Which has MapView)

import UIKit
import MapKit
class Map2ViewController: UIViewController
{
    var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
    @IBOutlet weak var dispAnnotation: MKMapView!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        print("The values received are \(ar)")
    }   
}

Please see: The segue name is "drawmap". The story board has SearchBloodViewController->NavigationController->Map2ViewController

On Map2ViewController , in this line:

var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()

You're creating an array with a default value (a new instance of an array with data type CLLocationCoordinate2D), so the content of ar is overriten by an empty array when you are loading this controller, you just need to create a nullable var, that can be assigen through the segue:

var ar : [CLLocationCoordinate2D]?

Everything else looks fine.

EDIT: ar is an optional var, so if you want to print it's content you need to unwrap it like this:

if let notNullArray = ar {
  print("The values received are \(notNullArray)")
}
let controller = navigationController.topViewController as! Map2ViewController

When above line is called your viewDidLoad will be called then only as your navigation controller will present Map2ViewController. Therefore it will print a blank array in your viewDidLoad method of Map2ViewController. I recommend you to pass the array in initializer method of Map2ViewController.

The above method is for using without storyboards. For your case I would recommend you to do whatever you want in setter method of ar variable of your Map2ViewController.

You can do it as follows:

var ar: [CLLocationCoordinate2D] = {
     didSet {
       print("The values received are \(ar)")
     }
}

Got my bug fixed. Actually I am setting all the values of coordinates in my Search Action. What I am doing now is I took entire logic that was there in Search action and put it in Prepareforsegue section. So now my Search action is empty and just doing one thing which is letting me go to the next screen which is map2ViewController. where as the whole computation and setting of longitude and latitude code has been put in prepare for segue method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM