gpt4 book ai didi

ios - 从另一个函数 : mapView = nil 访问我的 mapView var

转载 作者:行者123 更新时间:2023-11-28 06:48:36 25 4
gpt4 key购买 nike

我试图通过函数在 mapView 上放置一些注释,但 mapView 在调试器中显示为 nil。我从不同的类调用下面的函数,调用似乎通过设置一些断点来工作。它只是在尝试将注释数组添加到 mapView 时出错。

let temp = MapViewController()
temp.createCurrentSevereAnnotations()

这是我的 MapViewController 代码:

import UIKit
import MapKit

final class MapViewController : UIViewController, MKMapViewDelegate{

@IBOutlet weak var mapView: MKMapView!

// Change the Map Type from the tool bar segmented button.
//this function works fine. mapView is not nil.
@IBAction func changeMapType(sender: UISegmentedControl) {
switch (sender.selectedSegmentIndex) {
case 0:
mapView.mapType = .Standard
case 1:
mapView.mapType = .Hybrid
default:
mapView.mapType = .Satellite

} //^ this func works fine
}
//Puts an array of CustomPointAnnotation on the map.
func createCurrentSevereAnnotations() {
var annotations: Array = [CustomPointAnnotation]()
let severeReports = currentSevereReports

for item in severeReports
{
let annotation = CustomPointAnnotation(latitude: Double(item.lat!), longitude: Double(item.lon!))
print("The lat is: \(item.lat!)")
print("The lon is: \(item.lon!)")
switch(Int(item.type!))
{
case 1:
annotation.imageName = "ef0"
annotation.type = "Tornado"
break
case 2:
annotation.imageName = "wind"
annotation.type = "Wind"
break
default:
annotation.imageName = "hail"
annotation.type = "Hail"
break
}
print("item type : \(annotation.type)")
annotation.title = item.magnitude
annotation.subtitle = item.location
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
annotation.time = item.time
annotation.county = item.county
annotation.remarks = item.remarks

annotations.append(annotation)
}
print("Annotations count = \(annotations.count)") //<- works
mapView.addAnnotations(annotations) //<- this is where mapView is nil
}


// The class for our CustomPointAnnotation
class CustomPointAnnotation: NSObject, MKAnnotation {
var title: String? //mag
var subtitle: String? //location
var latitude: Double
var longitude:Double
var imageName: String!
var time: String?
var remarks : String?
var county: String?
var type: String?
var comments: String?


var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}

init(latitude: Double, longitude: Double) {
self.latitude = latitude
self.longitude = longitude
}
}
}

最佳答案

问题就在这里

let temp = MapViewController()

不是实例化 View Controller 的方式。


您应该使用从 Storyboard 中加载。 (click here to check apple documentation)

func instantiateViewControllerWithIdentifier(_ identifier: String) -> UIViewController

尝试用这种方式实例化temp。

let temp = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as UIViewController
self.presentViewController(temp, animated: true, completion: nil)

您应该为您的 View Controller 定义一个标识符(我在示例中使用“MapViewControllerIdentifier”) ,您可以在应用的 storyboard 的 View 中执行此操作。

关于ios - 从另一个函数 : mapView = nil 访问我的 mapView var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35544221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com