错误,不能在viewController类型上使用实例成员。实例成员不能用于viewController类型。

问题描述 投票:0回答:1

我得到这个错误。

实例成员'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)
}

storyboard

ios swift xcode mapkit
1个回答
1
投票

正如错误告诉你的那样。MapViewController 是一个类型,它并不是一个实例 MapViewController - Swift 习惯用大写字母表示类型,用小写字母表示变量,这也有助于你发现这类错误。

你似乎没有一个关于 MapViewController 在你 ContentViewController所以我不确定你想把注释添加到哪个地图上。

也许你想在地图上添加一个新的 MapViewController 当用户选择一个单元格时,地图会显示在哪里? 也许地图已经显示在其他地方了?

您需要您的 ContentViewController 以获得对您的 MapViewController 实例无论在哪里,都是为了调用函数来设置注解。

如果你正在展示一个新的控制器,那么你将使用一个转场,并在 prepare(for:).

© www.soinside.com 2019 - 2024. All rights reserved.