gpt4 book ai didi

ios - Swift - 向我的 MKPointAnnotation 添加一个按钮

转载 作者:搜寻专家 更新时间:2023-11-01 07:30:04 25 4
gpt4 key购买 nike

在 Swift 1.2 上:我想在 map 上的注释中添加一个按钮。首先点击 pin 显示用户的 id,这是有效的,但随后,通过按下按钮,我想将用户发送到详细 View Controller 。

你可以知道我在找什么herehere

尝试应用这些解决方案,但我认为我遗漏了一些东西。这是我正在寻找的另一个例子

enter image description here

这是我的类(class)声明:

import UIKit
import MapKit
import CoreLocation
import Parse

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIActionSheetDelegate {

这是我想用按钮创建注释的代码部分:

    // MARK: - Create Annotation

func createAnnotations(point: PFGeoPoint, address: String)
{
println("*** this evaluates n.3 ***")
var query = PFUser.query()


query?.whereKey("location", nearGeoPoint: point, withinKilometers: withinKms)
query?.orderByAscending("location") //MARK: Put list in order

//MARK: Not include current user on the map
var me = PFUser.currentUser()?.username
query?.whereKey("username", notEqualTo: me!)

query?.limit = self.kLimitNumberForQueryResults


query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if error == nil
{
for(var i = 0; i < objects!.count; i++) {

let user = objects![i] as! PFUser
var myHomePin = MKPointAnnotation()
let userPoint = user["location"] as! PFGeoPoint
myHomePin.coordinate = CLLocationCoordinate2DMake(userPoint.latitude, userPoint.longitude)
myHomePin.title = user.username

//MARK: core of the problem
// let detailButton : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
// myHomePin.rightCalloutAccessoryView = detailButton
//




// myHomePin.subtitle = address
self.mapView.addAnnotation(myHomePin)
// self.closeUsersArray.append(user.username!) //MARK: Test

}






// println("this is the array of users * \(self.closeUsersArray) *") //MARK: Test
}
else
{
println("Error: " + error!.localizedDescription)
}

})

}

提前致谢!

最佳答案

使用“viewForAnnotation” map View 委托(delegate)方法设置左右标注​​附件。并且 UIButton.buttonWithType 也被替换,这将起作用:编辑:您需要的所有委托(delegate)方法但我没有通过您的 createAnnotation 函数,因为您说它工作正常

class MapViewController: UIViewController, MKMapViewDelegate //
{
@IBOutlet weak var mapView: MKMapView!{ //make sure this outlet is connected
didSet{
mapView.delegate = self
}
}

// MARK: - Map view delegate

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationViewWithIdentifier("AnnotationView Id")
if view == nil{
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView Id")
view!.canShowCallout = true
} else {
view!.annotation = annotation
}

view?.leftCalloutAccessoryView = nil
view?.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure)
//swift 1.2
//view?.rightCalloutAccessoryView = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton

return view
}

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
//I don't know how to convert this if condition to swift 1.2 but you can remove it since you don't have any other button in the annotation view
if (control as? UIButton)?.buttonType == UIButtonType.DetailDisclosure {
mapView.deselectAnnotation(view.annotation, animated: false)
performSegueWithIdentifier("you're segue Id to detail vc", sender: view)
}
}
}

//Your function to load the annotations in viewDidLoad

}

关于ios - Swift - 向我的 MKPointAnnotation 添加一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33123724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com