Ios Mapview nil闭包,查看有关StackO的主题。但是没有';我帮不了我
我已经阅读了这个主题,用户让我检查一下 但这对我没有帮助,地图视图仍然是零。 我也读过这篇文章,但没有帮助 任何人都可以查看我的代码并帮助我,而不是链接到另一个毫无帮助的主题吗 我在类驱动程序中有这个函数Ios Mapview nil闭包,查看有关StackO的主题。但是没有';我帮不了我,ios,swift,mkmapview,Ios,Swift,Mkmapview,我已经阅读了这个主题,用户让我检查一下 但这对我没有帮助,地图视图仍然是零。 我也读过这篇文章,但没有帮助 任何人都可以查看我的代码并帮助我,而不是链接到另一个毫无帮助的主题吗 我在类驱动程序中有这个函数 private static let _instance = Driver(); static var Instance: Driver { return _instance; } func getAllDriversLocations(){ let storyboard
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)
}
}
}
})
}
这里我调用placeAllCabPlacemarks,位置数组在placeAllCabPlacemarks函数中得到了正确的结果,但是我在这里遇到了mapView为零的问题:
@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)
}
}
我真的检查了所有的东西,还有互联网上的一些东西&stackoverflow。
有人能检查一下为什么这不起作用吗?
主要问题是地图视图为零…所以我在上一篇文章中链接到的答案所在的问题 第一个问题是,我在以下位置使用了单例:
Driver.Instance.getAllDriversLocations()
我改为:
let driver = Driver() //instance of the class driver
driver.getAllDriversLocations(callBack: self)
第二个问题是这个代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
我将代码更改为:
func getAllDriversLocations(callBack : ViewController){
let vc = callBack
}
我希望有一天我能帮助别人,没有像我链接到的上一篇文章那样让事情变得更糟。你在情节提要中连接了地图代理吗?是的,我从情节提要中的viewcontroller连接了IBOutlet@mat@mat这与闭包有关..您是否检查过
viewDidLoad
中的myMap
是否为nil
是否执行myMap?.delegate=self
?是的,存在mapview,但从vc.placeAllCabPlacemarks(数组:位置)检查时,它为零。该功能的故事板正在运行。但是mapview nil@Matthewsheam