Я делаю аннотацию, которая включает настраиваемую кнопку. Когда эта кнопка нажата, она должна что-то делать. Сначала попробую распечатать. Как это работает сейчас, представление аннотаций закрывается, ничего не печатая. Его закрытие, потому что я делаю button.removeFromSuperview. Вот как это выглядит очень кратко, и сосредоточьтесь только на кнопке
class CustomAnnotation: MKPinAnnotationView, MKMapViewDelegate, CLLocationManagerDelegate {
let infoButton : UIButton = UIButton.init(frame:CGRect(x: 250, y: -300, width: 40, height: 40))
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(true, animated: animated)
if(selected) {
infoButton.setImage(UIImage(named:"home.png"), for: .normal)
infoButton.addTarget(self, action: #selector(sayHello), for:.touchUpInside)
self.addSubview(infoButton)
} else {
infoButton.removeFromSuperview()
}
func sayHello(sender: UIButton!) {
print("I should print something...")
}
}
Все в настроенном виде аннотаций выглядит великолепно. Я вызываю класс CustomAnnotation в свой Viewcontroller в функции viewForAnnotation.