2017-04-18 1 views
0

ich dieses Thema gelesen haben, die mich Benutzer lassen How to access ViewController component from PageViewController überprüfenMapview nil Verschluss, Säge Themen auf StackO. aber half mir nicht

Aber es half mir nicht, und die mapView ist noch gleich Null. Ich lese auch das, aber hat nicht geholfen http://www.apeth.com/swiftBook/ch04.html#SECinstanceRefs Könnte jemand bitte meinen Code überprüfen/sehen und mir helfen, anstatt zu einem anderen Thema zu verlinken, das überhaupt nicht hilft?

Ich habe diese Funktion in der Klasse Treiber

private static let _instance = Driver(); 

static var Instance: Driver { 
    return _instance; 
} 

func getAllDriversLocations(){ 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController; 

    var location : [CLLocation] = [] 

    Account.Instance.dbRef.child("driverLocations").observeSingleEvent(of: .value, with: { (snapshot) in 
     if let data = snapshot.value as? NSDictionary { 
      if let loca = data as? [String: [String:Any]]{ 
       let keys = loca.keys 
       let sortedKeys = Array(keys).sorted(by: { $0 > $1 }) // highest will be the first 
       _ = sortedKeys[0] 
       //let intHighPlus = Int(highestKey)! + 1 
       for index in 0...sortedKeys.count-1{ 

        let stringIndex = "0\(index+1)" 
        if let indexChecked = loca[stringIndex]{ 
         if let doubleLatString = (indexChecked["latitude"]){ 
          let latDouble = (doubleLatString as! NSString).doubleValue 

         if let doubleLongString = (indexChecked["longitude"]){ 
          let longDouble = (doubleLongString as! NSString).doubleValue 
          location.append(CLLocation(latitude: (latDouble), longitude: (longDouble))) 
          } 
         } 
        } 
       } 
       if location.count > 0{ 
        vc.placeAllCabPlacemarks(array: location) 
       } 
      } 
     } 
    }) 
} 

Also hier nenne ich placeAllCabPlacemarks, die Lage Array wird immer korrekt in der placeAllCabPlacemarks Funktion aber dann bekomme ich das Problem, dass mapView nil hier:

@IBOutlet weak var myMap: MKMapView? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    myMap?.delegate = self 
    initializeLocationManager() 
    Driver.Instance.getAllDriversLocations() 
} 
func placeAllCabPlacemarks(array: [CLLocation]){ 
    //print("locs: \(array)") 
    let arraySize = array 
    for index in 0...arraySize-1 { 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = CLLocationCoordinate2D(latitude: array[index].coordinate.latitude, longitude: array[index].coordinate.longitude) 
      print("map \(myMap)") //Here it's nil 
      myMap?.addAnnotation(annotation as MKAnnotation) 
     } 
} 

Ich habe wirklich alles überprüft, auch ein paar Artikel im Internet & stackoverflow. Könnte jemand bitte überprüfen, warum das nicht funktioniert? Das Hauptproblem ist, dass die mapView Null ist ...

+0

haben Sie haben den Map-Delegaten im Storyboard verbunden? – mat

+0

Ja Ich verband das IBOutlet von der Viewcontroller im Storyboard @mat – royvano

+0

@mat es hat etwas mit der Schließung zu tun .. – royvano

Antwort

0

Also das Problem, dass die Antworten, die ich in der früheren Post verlinkt ...

Das erste Problem war, dass ich verwendet Singletons an:

Driver.Instance.getAllDriversLocations() 

ich geändert, dass auf:

let driver = Driver() //instance of the class driver 
driver.getAllDriversLocations(callBack: self) 

Das zweite Problem dieser Code war:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController; 

änderte ich diesen Code zu diesem:

func getAllDriversLocations(callBack : ViewController){ 

    let vc = callBack 
} 

ich, dass ich mit diesem einen Tag helfen jemand hoffen, statt es am schlimmsten, wie die früheren Post zu machen habe ich habe im Zusammenhang mit ..