私はこの質問が複数回尋ねられていることを知っていますが、すべての回答は私のアプリで起こっていることとは若干異なるようです。ViewForAnnotationが呼び出されないのはなぜですか?
mapViewのdelegateがViewControllerに設定され、マップビューがmapView領域内に表示されるように注釈がマップに追加されると、viewForAnnotation関数が呼び出されます。
は現在、私は1 MKMapView( のMapView) これのViewControllerはMapViewの中で表示される4つの異なるマップを制御が含まれている一つのメインのViewController(mainVC)を持っています。func moveViews(sender:Int) {
// This function handles which button on the Segmented Control was clicked and the loads the appropriate map into the mapView (passed as a para
removeAnnotationsAndOverlays() // ~~~
if sender == 0 {
// First Map was selected
let map1VC = map1VC()
map1VC.loadMap1View(mapView)
map1VC.centerMapOnLocation()
}
else if sender == 1 {
// Second Map was selected
let map2VC = map2VC()
map2VC.loadMap2View(mapView)
}
else if sender == 2 {
// Third Map was selected
let map3VC = map3VC()
map3VC.loadMap3View(mapView)
}
else if sender == 3 {
// Fourth Map was selected
let map4VC = map4VC()
map4VC.loadMap4View(mapView)
}
else {
// Load First Map as default
let map1VC = map1VC()
map1VC.loadMap1View(mapView)
map1VC.centerMapOnLocation()
}
}
異なるマップの各機能を制御し、いくつかの異なるクラスがあります。
- 地図1が - から読み込まれます(MKAnnotationから継承)MKPolylinesとカスタム注釈の組み合わせを表示しますplist - これは素晴らしい作品です!
- マップ2 - plistから読み込まれた複数のMKPolylinesを表示します - これは素晴らしい動作です!
- マップ3 - plistから読み込まれた複数のMKPolylinesを表示します - これは素晴らしい動作です!
- マップ4 - 複数のカスタムアノテーションを表示する必要があります - これは動作しません!ここで
地図4で何が起こっているかである。
MKMapViewが正しく
var mapView: MKMapView = MKMapView() // declared as a global variable/object inside the map4VC() // Initial function to set up Map // Think of this function as the "override func viewDidLoad() {}" func loadMap4View(mV: MKMapView) { // This connects the parameter passed (mV) and sets it as the delegate for the mapView used throughout this file // In other words, it allows the mapView on the MainVC to use all of the code in this file to handle different actions mapView = mV mapView.delegate = self let initialLocation = CLLocation(latitude: 50.3603125, longitude: 2.794017) // calculates the region you'll look at on the screen let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate, regionRadius, regionRadius) // sets the region of the map mapView.setRegion(coordinateRegion, animated: true) //addPins() // This can use a custom function to load all of the Pins //mapView.addAnnotations(coords.allLocations!) // This line also works to add all of the individual pins mapView.addAnnotation(coords.allLocations![2]) // This adds one single Pin
}
をロードされている(現在のクラスへのMapViewのデリゲートを設定しますmapView.delegate = self)
- それは(mapView.setRegion(coordinateRegion、アニメーション:真))適切な位置にズームイン
クラスはプロパティリストから読み出して(ヘルパークラスを使用して)カスタムMKAnnotations(CemAnno)のアレイを構築
場所はCemAnnoの呼び出さallLocationsのアレイに記憶されている:
var allLocations: [CemAnno]? = [] // This is declared in the helper class along with a bunch of other stuff that grabs all of the information from a plist class CemAnno: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var casualties: String? var visitInfo: String? var histInfo: String? var region: String? init(title: String, coordinate: CLLocationCoordinate2D, region: String, casualties: String, visitInfo: String, histInfo: String) { self.coordinate = coordinate self.title = title self.region = region self.casualties = casualties self.visitInfo = visitInfo self.histInfo = histInfo }
}
// This builds an object inside the the map4VC() class called coords that holds all of the information collected from the plist var coords = BuildCoordinates(filename: "Coordinate")
EACを追加しますこれらのうちの1つがマップ(mapView.addAnnotations)にピンとして表示されます(表示されています) mapView.addAnnotation(coords.allLocations![2])//これは1つのピンアノテーションを追加しますviewForAnnotation機能がこの作品
呼び出すことはありません、しかし、私が表示されているが、ViewForAnnotation関数が呼び出されることはありません注釈をカスタマイズしようとしている????
// This function is NEVER called
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
// Define a reuse identifier. This is a string that will be used to ensure we reuse annotation views as much as possible.
let identifier = "CemAnno"
// Check whether the annotation we're creating a view for is one of our CemAnno objects.
if annotation.isKindOfClass(CemAnno.self) {
print("correct class")
//let anno = annotation as! CustomAnnotation
// Try to dequeue an annotation view from the map view's pool of unused views.
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
print("no reusable view")
// If it isn't able to find a reusable view, create a new one using MKPinAnnotationView and sets its canShowCallout property to be true. This triggers the popup with the name.
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
// Create a new UIButton using the built-in .Custom type. This is so we can add an image to the button.
let btn = UIButton(type: .DetailDisclosure)
//btn.setImage(anno.image, forState: .Normal)
annotationView!.rightCalloutAccessoryView = btn
} else {
print("reusing a view")
// If it can reuse a view, update that view to use a different annotation.
annotationView!.annotation = annotation
}
return annotationView
}
// If the annotation isn't from a CustomClass, it must return nil so iOS uses a default view.
return MKPinAnnotationView()
}
私は、マップの現在のビュー領域の内側のピンの位置を追加しようとしましたが、ViewForAnnotationは解雇されることはありません。 ピンの位置をマップの現在のビュー領域外に追加しようとしましたが、ViewForAnnotationは決して実行されません - これは動作するものでなければならず、マップをスクロールして内側に表示されるはずです関数を起動する現在のビュー領域ですが、そうではありません(ピンが表示され、マップに表示されることに注意してください)。
これは、私がしたいピンをカスタマイズできないようにします。
私は完全に正常に動作地図1と同様の手法を使っていますが、ViewForAnnotationを地図4.
内部で呼び出されることはありません何らかの理由で任意の提案をいただければ幸いです!
いくつかのコードを、私は、コードを追加しました – shinoys222
を助けるかもしれません。今度は、Map1からMap4に変更してMap1に戻ったときには、自分の注釈が私が望む画像で表示されていることがあります。それ以外の時はピンでしか表示されません。 –
私は複数のもので遊んできました。そして、私はそれがデリゲートと何か関係があるはずだと思います。デリゲートは設定されていますが、func loadMap4View(mV:MKMapView)メソッドの最初の行に設定されているにもかかわらず、表示されているMKMapViewのインスタンスに正しく設定されていないかのようです。別のクラスに渡されているmVパラメータと関連がありますか(参照または型渡し)???何か案は? –