ios - 错误:实例成员不能用于类型 viewController
问题描述
我得到这个错误,
实例成员“setPlacesAnnotations”不能用于“MapViewController”类型;你的意思是使用这种类型的值吗?
线上
mapViewController.setPlacesAnnotations()、mapViewController.setBuildingsAnnotations() 和 mapViewController.setRecreationAnnotations()。
我将如何解决这个问题以便代码运行?此代码的主要目标是让用户单击集合视图中的单元格,然后根据所选单元格类别在地图上放置注释。
这是我的集合视图代码。
class ContentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
let imageArray = [UIImage(named: "1"), UIImage(named: "2"), UIImage(named: "3")]
let imageNameArray = ["Image 1", "Image 2", "Image 3"]
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.mapIconImage.image = imageArray[indexPath.row]
cell.mapIconLabel.text! = imageNameArray[indexPath.row]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch (indexPath.item) {
case 0: MapViewController.setPlacesOfWorshipAnnotations()
case 1: MapViewController.setOrganizationsAnnotations()
case 2: MapViewController.setGroceriesAnnotations()
}
}
class CustomCell: UICollectionViewCell {
@IBOutlet weak var mapIconImage: UIImageView!
@IBOutlet weak var mapIconLabel: UILabel!
}
这是我的地图视图的代码。
struct PlacesOnMap {
var name: String
var latitude: Double
var longitude: Double
init(name: String, latitude: Double, longitude: Double) {
self.name = name
self.latitude = latitude
self.longitude = longitude
}
}
class MapViewController: UIViewController {
@IBOutlet var mapView: MKMapView!
var locationManager = CLLocationManager()
var currentCoordinate: CLLocationCoordinate2D!
var places = [PlacesOnMap(name: "place 1", latitude: 28.551700, longitude: -81.374800),
PlacesOnMap(name: "place 2", latitude: 28.553018, longitude: -81.374206),
PlacesOnMap(name: "place 3", latitude: 28.553019, longitude: -81.367839)
]
var buildings = [PlacesOnMap(name: "place 1", latitude: 28.556969, longitude: -81.364319),
PlacesOnMap(name: "place 2", latitude: 28.558126, longitude: -81.364725)
]
var recreation = [PlacesOnMap(name: "place 1", latitude: 28.54693, longitude: -81.393071),
PlacesOnMap(name: "place 2", latitude: 28.538523, longitude: -81.385399),
PlacesOnMap(name: "place 3", latitude: 28.542817, longitude: -81.378117),
PlacesOnMap(name: "place 4", latitude: 28.538985, longitude: -81.404694)
]
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.startUpdatingLocation()
}
func setPlacesAnnotations() {
let places = places.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(places)
}
func setBuildingsAnnotations() {
let places = buildings.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(places)
}
func setRecreationAnnotations() {
let places = recreation.map { placeOnMap -> MKPointAnnotation in
let place = MKPointAnnotation()
place.coordinate = CLLocationCoordinate2D(latitude: placeOnMap.latitude, longitude: placeOnMap.longitude)
place.title = placeOnMap.name
return place
}
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(places)
}
解决方案
正如错误告诉你的那样MapViewController
,它是一个类型,它不是一个实例MapViewController
- Swift 约定使用大写字母表示类型,小写字母表示变量也可以帮助你发现这些类型的错误。
MapViewController
您的中似乎没有 的实例ContentViewController
,因此我不确定您要将注释添加到哪个地图。
也许您想MapViewController
在用户选择一个单元格时呈现一个新的?也许地图已经显示在其他地方?
您需要ContentViewController
引用您的MapViewController
实例,无论在哪里,以便调用该函数来设置注释。
如果您正在展示一个新控制器,那么您将使用 segue 并将注释设置为prepare(for:)
.
推荐阅读
- python - Python tkinter:将滚动条粘贴到文本小部件上,看起来好像它们是一个小部件
- html - 为什么第一个 div 及其图像没有被渲染?
- java - 使用 CloseableHttpClient 而不是 ClientBuilder
- algorithm - GraphFrame 最短路径因未找到 Vertex Id 错误而失败
- jquery - 为什么 Node.js 上会出现“TypeError:client.db 不是函数”,我该如何解决?
- python - Tensorflow:简单的图像图像分类器在时代之间根本不更新
- typescript - 在 typeorm 中保存和使用自引用实体
- python - 如何避免使用 concurrent.futures() 的最大内存分配?
- android - 使用 OkHttp 对站点进行授权
- python - Lambda 层:函数构建代码之外的操作正在传递一个“图形”张量