2

1) I have the ViewController with the MapKit

1.1) I have added some pin's to Map

class ViewController: UIViewController, MKMapViewDelegate

2) I write the new classes for custom pin Callout and Annotation

class CustomPointAnnotation: MKPointAnnotation {

class CustomCalloutView: UIView {

3) I haved created .xib for my custom pin callout enter image description here

4) I created the button in my .xib, this button must do something, for example

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

5) This button doesn't work

What is usually practice for this? I want to make my button working. Button is blue: enter image description here

All project you can see on Github: https://github.com/genFollowMe1/forStack Thank you, sorry for English )

HardikDG
  • 5,892
  • 2
  • 26
  • 55
GenRiH
  • 132
  • 2
  • 11

2 Answers2

4

for Objective C: https://github.com/nfarina/calloutview

Update

for swift

subclass the MKAnnotationView for custom call out and override the hitTest: and pointInside: methods.

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
}
}
Jeyamahesan
  • 1,101
  • 7
  • 15
  • Sorry, your solution is in Objective-C, i use Swift. I have custom callout and i can send variables to my .xib, but cant make working button.. `// init XIB` `let customView = (NSBundle.mainBundle().loadNibNamed("ViewCallout", owner: self, options: nil))[0] as! CustomCalloutView;` `let calloutViewFrame = customView.frame;` `let cpa = view.annotation as! CustomPointAnnotation` `cpa.name = "Company Name"` `customView.name.text = cpa.name` `view.addSubview(customView)` – GenRiH Apr 08 '16 at 13:59
  • you have to subclass MKAnnotationView for the customcalloutview(now your subclassing from UIView), add the view, buttons, lables in that subclass and override the hitTest: and pointInside: methods to achieve the button click. – Jeyamahesan Apr 11 '16 at 07:41
  • You're right, i feeling it is right way. Do not tell me where to see manual for this overrided methods?? (hitTest and pointInside) – GenRiH Apr 11 '16 at 10:19
0

The way you have to do it's to make a reference of the button from your .xib into the associated .swift file.

In your ViewController you do something like this :

let button:UIButton!

[...]

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

func tappedButton() {
    print("Taped !")
}
Ragnar
  • 2,550
  • 6
  • 36
  • 70
  • Thank you, i made about what you said, but get the error: http://i.imgur.com/G1cD3Hz.png – GenRiH Apr 08 '16 at 13:16
  • Sorry my bad. You have got to load your xib file as a subView. Look here : https://stackoverflow.com/questions/24857986/load-a-uiview-from-nib-in-swift#26326006 and from here if `subView` is the name of your loaded xib file. `subView.button.targetForAction ...` – Ragnar Apr 08 '16 at 13:22
  • Yes, i have loaded: `code` let customView = (NSBundle.mainBundle().loadNibNamed("ViewCallout", owner: self, options: nil))[0] as! CustomCalloutView;`code` But i cant finally understand, can you please look my project on github? let calloutViewFrame = customView.frame; – GenRiH Apr 08 '16 at 13:26
  • You have to put this code inside your viewController but your .xib file need it's own .swift file. look here https://www.youtube.com/watch?v=r5SKUXSuDOw – Ragnar Apr 08 '16 at 13:27