Ios 如何判断何时按了swift 4键
嘿,伙计们,我真的被卡住了,我还没有找到答案,所以我们开始吧 我有一个地图视图,需要显示多个注释,所以我制作了一个数组,并从一个帖子数组中存储标题、子标题等,这些帖子都是用户在地图上共享的帖子。这段代码运行良好Ios 如何判断何时按了swift 4键,ios,swift,xcode,annotations,mapkit,Ios,Swift,Xcode,Annotations,Mapkit,嘿,伙计们,我真的被卡住了,我还没有找到答案,所以我们开始吧 我有一个地图视图,需要显示多个注释,所以我制作了一个数组,并从一个帖子数组中存储标题、子标题等,这些帖子都是用户在地图上共享的帖子。这段代码运行良好 @IBOutlet weak var mapView: MKMapView! var annotations = [MKAnnotation]() var posts = [Post]() // arr of local post objects func addPostsToMa
@IBOutlet weak var mapView: MKMapView!
var annotations = [MKAnnotation]()
var posts = [Post]() // arr of local post objects
func addPostsToMap(){
mapView.removeAnnotations(annotations)
annotations.removeAll()
for post in posts{
let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(post.lat, post.lon)
let annotation = PostAnnotation(title: post.userName , subtitle: post.postContent, coordinate: location)
annotations.append(annotation)
}
mapView.addAnnotations(annotations)
}
以下是定制的邮资说明:
class PostAnnotation : NSObject , MKAnnotation{
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D
init(title : String , subtitle : String , coordinate : CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
}
}
问题如何判断按下了哪个批注??我发现我可以使用mapkit委托方法定义自定义注释,但是没有像表视图那样的indexPath,那么我如何判断按下了哪个 在postnotation中添加一个额外字段,该字段是posts数组中的post索引 然后在mapViewMKMapView的实现中,didSelect:MKAnnotationView您只需要获取所点击视图的注释,然后使用索引从数组中获取帖子
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let postAnnotation = view.annotation as? PostAnnotation else {
return
}
let post = posts[postAnnotation.index]
// do something with post
}