我尝试在每个注释 View 中创建一个按钮,将用户带到根据所选注释自定义的新页面。我已经成功实现了 map View 来显示带有从解析数据库加载的标题和副标题的注释,但正在努力寻找一种方法 1) 在注释 View 中添加按钮以将用户带到新 View 2) 寻找一种方法为每个选定的注释创建自定义 View 。任何帮助将不胜感激?
代码
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var MapViewLocationManager:CLLocationManager! = CLLocationManager()
let btn = UIButton(type: .DetailDisclosure)
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.delegate = self
MapViewLocationManager.delegate = self
MapViewLocationManager.startUpdatingLocation()
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
override func viewDidAppear(animated: Bool) {
let annotationQuery = PFQuery(className: "Clubs")
annotationQuery.findObjectsInBackgroundWithBlock {
(clubs, error) -> Void in
if error == nil {
// The find succeeded.
print("Successful query for annotations")
// Do something with the found objects
let myClubs = clubs! as [PFObject]
for club in myClubs {
//data for annotation
let annotation = MKPointAnnotation()
let place = club["location"] as? PFGeoPoint
let clubName = club["clubName"] as? String
let stadiumName = club["stadium"] as? String
annotation.title = clubName
annotation.subtitle = stadiumName
annotation.coordinate = CLLocationCoordinate2DMake(place!.latitude,place!.longitude)
//add annotations
self.mapView.addAnnotation(annotation)
}
} else {
// Log details of the failure
print("Error: \(error)")
}
}
}
最佳答案
对于第一个 - 您可以在 annoationView 上使用 leftCalloutAccessoryView 和 rightCalloutAccessoryView。您必须实现 MapViewDelegate mapView(_:viewForAnnotation:)。
对于第二个 - 与calloutAccessoryView类似,您可以访问annoationView上的detailCalloutAccessoryView。您可以使用它来自定义标注。
关于ios - 如何从每个注释 View 创建自定义标注按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35037827/