0

I was reading the thread: IBOutlet of another view controller is nil

I have a problem very similar to that.

RequestViewController

class DenunciasResueltasViewController: UIViewController {

    @IBOutlet var mapView: GMSMapView!
    var solicitudes = [SolicitudesModel]()
    var tempMap: GMSMapView!

        override func viewDidLoad() {
            super.viewDidLoad()

            let camera = GMSCameraPosition.camera(withLatitude: 3.4824182, longitude: -8.1776567, zoom: 15)
            self.mapView.camera = camera
    }

func recenterMap(latitude:Float!, longitude:Float!) -> Void {

        let coordinates = CLLocationCoordinate2DMake(CLLocationDegrees(latitude), CLLocationDegrees(longitude))
        mapView = tempMap
        self.mapView.animate(toLocation: coordinates)
    }

RequestTableViewController

class RequestTableViewController: UITableViewController {
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

//Some code to fill the table


        let viewController = self.storyboard?.instantiateViewController(withIdentifier: "RequestVC") as! RequestViewController
        viewController.recenterMap(latitude: solicitudes[indexPath.row].getLatitude(), longitude: solicitudes[indexPath.row].getLongitude() )

        return cell
    }
}

Both components are initialized at same time in runtime, I mean, both are part of the same View.

And when the users click in a 'row' I wanna update the mapView

for that resason, I'm using the method 'RecenterMap'

But, the variable 'self.mapView' is always 'nil'. How I can update this value?

Community
  • 1
  • 1
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
  • Set a CLCoordinate property to know where to center the map, set it when you create the viewcontroller, and on viewDidLoaf or appear, call `recenterMap()` on that value. – Larme Feb 04 '17 at 19:32

4 Answers4

0

You should use the delegate method didSelectRowAtIndexPath to do the recentering, because this is called when the user tapps on a cell. It could be that the Outlets of the first viewcontroller haven't yet been set when the second starts to fill its cells.

Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
0

This is a common problem, wanting to access properties of a currently offscreen vc before you are going to present it.

You can solve it by forcing the view hierarchy to be build by accessing its view:

let _ = viewController.view

After this you are free to access any view-related property on the viewcontroller. So you should put it before you call .recenterMap

Simon
  • 2,419
  • 2
  • 18
  • 30
0

It's not really clear what are you trying to do: your code seem to instantiate a view controller with map for each table cell, but you don't actually present it after being instantiated.

You instantiate the RequestViewController, but it doesn't appear in the view hierarchy, thus the IBOutlet properties are not instantiated at that moment when you call them. Usually calling a view on a UIViewController instantiates all the outlets on it. Alternatively, you could call self.present(requestViewController) in order for it to be presented.

Vadim Popov
  • 1,177
  • 8
  • 17
0

What i understood is that, you have both the RequestViewController and RequestTableViewController, both are subview of a parent View,

1- your center on map function needs to be called on the delegate method :

didSelectRowAtIndexPath:

2- You need a reference to the RequestViewController and DO NOT INSTANTIATE IT FROM THE STORYBOARD. as you will get a new instance and not the one that is displayed already.

TheFuquan
  • 1,737
  • 16
  • 18