2016-04-08 14 views
2

1)私はMapKit(注釈)

1.1でのViewControllerを持っている)、私はいくつかのピンの

class ViewController: UIViewController, MKMapViewDelegate 

2をマップに追加している)私は私は私のカスタムピン吹き出し enter image description here

のために作成さ.xibをhavedカスタムピン吹き出しのための新しいクラスを作成し、注釈

class CustomPointAnnotation: MKPointAnnotation { 

class CustomCalloutView: UIView { 

3)

4)私は)このボタンは、例

@IBAction func clickTest(sender: AnyObject) { 
    print("aaaaa") 
} 

5のために、何かをしなければならない、私の.xibでボタンを作成し、このボタンは通常、このために練習している何

動作しませんか?私は自分のボタンを働かせたい。 ボタンは青です: enter image description here

あなたはGitHubの上で見ることができるすべてのプロジェクト:https://github.com/genFollowMe1/forStack が英語のため申し訳ありません、ありがとう)

答えて

2

Objective Cのために: https://github.com/nfarina/calloutview

更新迅速

サブクラスの

MKAnnotationView cuのためにhitTest:メソッドとpointInside:メソッドをオーバーライドしてオーバーライドします。

import UIKit 
import MapKit 

class CustomCalloutView: MKAnnotationView { 

@IBOutlet weak var name: UILabel! 

@IBAction func goButton(sender: AnyObject) { 

    print("button clicked sucessfully") 
} 

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { 
    let hitView = super.hitTest(point, withEvent: event) 
    if hitView != nil { 
     superview?.bringSubviewToFront(self) 
    } 
    return hitView 
} 

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
    let rect = self.bounds 
    var isInside = CGRectContainsPoint(rect, point) 
    if !isInside { 
     for view in subviews { 
      isInside = CGRectContainsPoint(view.frame, point) 
      if isInside { 
       break 
      } 
     } 
    } 
    return isInside 
} 
} 
+0

申し訳ありませんが、あなたのソリューションはObjective-Cです。私はSwiftを使用しています。私はカスタムコールアウトを持っています。私は.xibに変数を送ることができますが、作業ボタンを作ることはできません。 '// init XIB' ' let customView =(NSBundle.mainBundle()。loadNibNamed( "ViewCallout"、所有者:自己、オプション:nil))[0] as! CustomCalloutView; ' ' let calloutViewFrame = customView.frame; ' ' let cpa = view.annotation as! CustomViewAnnotation' 'cpa.name ="会社名 "' 'customView.name.text = cpa.name' ' view.addSubview(customView) ' – GenRiH

+0

customcalloutviewのMKAnnotationViewをサブクラス化する必要があります(UIViewのサブクラス化になりました)そのサブクラスのビュー、ボタン、ラベルを追加し、hitTest:およびpointInside:メソッドをオーバーライドしてボタンのクリックを実現します。 – Jeyamahesan

+0

あなたは正しいです、私はそれが正しい方法だと感じています。このオーバーライドされたメソッドのマニュアルをどこから参照するか教えてください。 (hitTestとpointInside) – GenRiH

0

あなたはそれがあなたの.xibからボタンの参照を行うことがありますしなければならない方法関連する.swiftファイルに追加します。あなたのViewControllerで

あなたはこのような何かを:

let button:UIButton! 

[...] 

button.targetForAction("tappedButton:", withSender: self) 

func tappedButton() { 
    print("Taped !") 
} 
+0

ありがとうございました、私はあなたが言ったことについて行われますが、エラーが発生します。http://i.imgur.com/G1cD3Hz.png – GenRiH

+0

申し訳ありませんが、私の悪いです。 xibファイルをサブビューとしてロードする必要があります。 ここをクリックしてください:https://stackoverflow.com/questions/24857986/load-a-uiview-from-nib-in-swift#26326006 ここから、 'subView'はロードされたxibファイルの名前です。 'subView.button.targetForAction ... ' – Ragnar

+0

はい、ロードしました:' code' let customView =(NSBundle.mainBundle()。loadNibNamed( "ViewCallout"、owner:self、options:nil))[0]として! CustomCalloutView; 'code' しかし、私は最終的に理解できません、あなたはgithubで私のプロジェクトを見てくださいできますか? let callViewViewFrame = customView.frame; – GenRiH