0

たとえば、さまざまな橋の位置に対応するように、マップにいくつかのピンを配置しました。これらの各ピンには、クリックするとタイトルとサブタイトルが表示される独自の注釈があります。これらの注釈内に情報ボタンを追加しました。ただし、どのブリッジ情報ボタンが押されたかによって UiViewController に表示される情報が異なる新しい UiViewController を開く方法がわかりません。

したがって、基本的には次の方法を知る必要があります。1: 注釈の情報ボタンが押されたときに UiViewController を開きます。2: UiViewController がどのブリッジ情報ボタンを押したかについての情報を変化させます。

これが私がこれまでに持っているものです:

     mapView.delegate = self



    //bridges
    var Bridge1 = CLLocationCoordinate2DMake(48.60,2.90)
    var bridge2 = CLLocationCoordinate2DMake(48.61, 2.91)
    var bridge3 = CLLocationCoordinate2DMake(48.62, 2.92)
    var bridge4 = CLLocationCoordinate2DMake(48.63, 2.93)
    var bridge5 = CLLocationCoordinate2DMake(48.64, 2.94)
    var bridge6 = CLLocationCoordinate2DMake(48.65, 2.95)


    var span = MKCoordinateSpanMake(0.4, 0.4)
    var region = MKCoordinateRegion(center: Bridge1, span: span)
    mapView.setRegion(region, animated: true)

    var Bridge1pin = MKPointAnnotation()
    Bridge1pin.coordinate = Bridge1
    Bridge1pin.title = "Bridge1"
    Bridge1pin.subtitle = "This is bridge 1"
    mapView.addAnnotation(Bridge1pin)

    var bridge2pin = MKPointAnnotation()
    bridge2pin.coordinate = bridge2
    bridge2pin.title = "Bridge2"
    bridge2pin.subtitle = "This is bridge 2"
    mapView.addAnnotation(bridge2pin)

    var bridge3pin = MKPointAnnotation()
    bridge3pin.coordinate = bridge3
    bridge3pin.title = "Bridge3"
    bridge3pin.subtitle = "This is bridge 3"
    mapView.addAnnotation(bridge3pin)

    var bridge4pin = MKPointAnnotation()
    bridge4pin.coordinate = bridge4
    bridge4pin.title = "Bridge4"
    bridge4pin.subtitle = "This is bridge 4"
    mapView.addAnnotation(bridge4pin)

    var bridge5pin = MKPointAnnotation()
    bridge5pin.coordinate = bridge5
    bridge5pin.title = "bridge5"
    bridge5pin.subtitle = "hello this is bridge 5"
    mapView.addAnnotation(bridge5pin)

    var bridge6pin = MKPointAnnotation()
    bridge6pin.coordinate = bridge6
    bridge6pin.title = "bridge6"
    bridge6pin.subtitle = "hello this is bridge 6"
    mapView.addAnnotation(bridge6pin)




}


func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "pin"
    var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if pin == nil {
        pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        pin!.pinColor = .Red
        pin!.canShowCallout = true
        pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
    } else {
        pin!.annotation = annotation
    }
    return pin
}


}
4

1 に答える 1

0

このリンクで、さまざまなビュー間のナビゲーションについて学ぶことができます。

また、ナビゲーションとナビゲーション用のデータの準備についてヒントを提供します。

1. ユーザーがブリッジをクリックしたときの検出

このためには、以下のようにcalloutAccessoryControlTappedという 1 つのコールアウト メソッドを実装する必要があります。

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    performSegueWithIdentifier("info", sender: view)
    // Above line of code is used for the segue operation between multiple MVCs.
}

2.セグエ操作を追加する必要があります

これは、プライマリ ビューからストーリー ボードの詳細ビューにコントロールをドラッグし、ポップアップで[表示] を選択し、属性インスペクターで識別子を情報として追加することで実行できます。

3.タップ付きブリッジのモデルを準備する

このためには、次のようにprepareForSegueメソッドをオーバーライドするだけです。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //Just validating by checking the same identifier 
    if (segue.identifier == "info") {
        if let annotation = sender as? MKAnnotationView {
            let detailViewController = segue.destinationViewController as! DetailViewController
            detailViewController.titleText  = annotation.annotation?.title ?? ""
            detailViewController.detaileText = annotation.annotation?.subtitle ?? ""
        }
    }
}

4. これが DetailViewController です。

タイトルサブタイトルを表示するためだけに、2 つのラベルが追加されました。

import UIKit
class DetailViewController: UIViewController {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!

    var titleText: String? { didSet { updateUI() } }
    var detaileText: String? { didSet { updateUI() } }

    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }

    private func updateUI() {
        self.titleLabel?.text = self.titleText
        self.detailLabel?.text = self.detaileText
    }
}
于 2016-08-01T06:31:08.237 に答える