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 ...
haben Sie haben den Map-Delegaten im Storyboard verbunden? – mat
Ja Ich verband das IBOutlet von der Viewcontroller im Storyboard @mat – royvano
@mat es hat etwas mit der Schließung zu tun .. – royvano