0

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

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • I think you don't need this line var ar: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]() becuase you have to print only var ar don't initialize again. – Bhadresh Mulsaniya Nov 24 '15 at 11:12
  • And you can also create the property of an array for the same and print that array so that you get answer. – Bhadresh Mulsaniya Nov 24 '15 at 11:26
  • @BhadreshMulsaniya : Got my bug fixed. Just see the answer I have given. I don't know if its the right approach but I am good for now. :) – Unbreakable Nov 24 '15 at 14:38

3 Answers3

1

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)")
}
Fantini
  • 2,067
  • 21
  • 32
  • It does not seem to be working: The new console output I get i "The values received are Optional([]) " – Unbreakable Nov 24 '15 at 11:21
  • I've updated the answer, you should read and learn about optional values on swift, this answer explains them really well, http://stackoverflow.com/questions/24003642/what-is-an-optional-value-in-swift – Fantini Nov 24 '15 at 11:28
  • Hello Fantini, I have kind of gone through that. But I guess there is some other bug. Even after trying the second method that you have suggested I am not getting the output. Thank you for the effort. Please let me know if you find any other mistake. – Unbreakable Nov 24 '15 at 11:30
  • the new output is "The values received are []" – Unbreakable Nov 24 '15 at 11:30
  • During debugging I noticed that my search Action is getting called long after the segue has executed this statement controller.ar = self.arra. The search method is hit later so I want my search action to be called before the segue executes this line "controller.ar = self.arra" – Unbreakable Nov 24 '15 at 12:12
  • I will need more information/code, 1. When does the search func is being called, 2. When does the segue triggers? – Fantini Nov 24 '15 at 12:31
  • - @IBAction func search(sender: UIButton) --- this is what represents the action related to search. Also the outlet is IBOutlet weak var bloodType: UITextField! – Unbreakable Nov 24 '15 at 12:33
  • so basically I have a empty page with a search button and text field – Unbreakable Nov 24 '15 at 12:34
  • now when user types the blood group it goes to the parse and fetches all the registered person with the matching blood type. – Unbreakable Nov 24 '15 at 12:35
  • I want to show those person location (stored in parse) in map view – Unbreakable Nov 24 '15 at 12:35
  • In my Map2ViewController I have outlet as @IBOutlet weak var dispAnnotation: MKMapView! .. I hope that's not an issue. Because value istself is not coming to the Map2ViewController. – Unbreakable Nov 24 '15 at 12:43
  • In the code mentioned above search action is present. – Unbreakable Nov 24 '15 at 12:53
  • 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. where as the whole computation and setting of longitude and latitude code has been put in prepare for segue method. – Unbreakable Nov 24 '15 at 14:31
  • Hope I am making sense here. And I hope you also understood my mistake. Am I doing things the right way now? But with this approach I am getting the desired values. – Unbreakable Nov 24 '15 at 14:35
0
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)")
     }
}
Jaycee
  • 159
  • 6
0

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.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171