Я создаю приложение с помощью быстрого доступа. Я пытаюсь передать информацию и вызвать функцию из файла класса пользовательского AnnotationClusterView в ViewController. Для этого я реализую делегата. Но я не знаю, где и как его инициализировать. Вот класс пользовательских AnnotationClusterView:Инициализировать делегата из файла класса
import Foundation
import MapKit
protocol MyDelegate {
func thatGuy(guy: Int)
}
class FBAnnotationClusterView : MKAnnotationView {
var delegate: MyDelegate!
var countLabel:UILabel? = nil
override init(annotation: MKAnnotation?, reuseIdentifier: String?){
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
let cluster:FBAnnotationCluster = annotation as! FBAnnotationCluster
for annot in cluster.annotations {
let fbannot: FBAnnotation = (annot as? FBAnnotation)!
imageArray.append(fbannot.image)
}
setupButtons()
}
required override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setupButtons() {
var xFactor = 1
for butt in self.imageArray {
let yFactor = CGFloat(xFactor * 40)
let buttButt: UIButton = UIButton(frame: CGRectMake(yFactor, 80, 16, 16))
buttButt.setImage(butt, forState: UIControlState.Normal)
buttButt.tag = xFactor
buttButt.addTarget(self, action: "thumbButt:", forControlEvents: UIControlEvents.TouchUpInside)
addSubview(buttButt)
xFactor++
}
}
func thumbButt(sender: UIButton) {
print("Button Pressed")
self.delegate?.thatGuy(sender.tag)
}
}
Вот код ViewController:
import MapKit
import UIKit
class ViewController: UIViewController, MKMapViewDelegate, MyDelegate {
override func viewDidLoad() {
...
}
func thatGuy(guy: Int) {
print("thatGuy func reached!")
}