Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-向我的MKPointAnnotation添加按钮_Ios_Swift_Mkmapview_Mkannotationview_Mkpointannotation - Fatal编程技术网

Ios Swift-向我的MKPointAnnotation添加按钮

Ios Swift-向我的MKPointAnnotation添加按钮,ios,swift,mkmapview,mkannotationview,mkpointannotation,Ios,Swift,Mkmapview,Mkannotationview,Mkpointannotation,关于Swift 1.2:我想在地图上的注释中添加一个按钮。首先点击pin显示用户的id,这正在工作,但是,通过按下按钮,我想将用户发送到详细视图控制器 你可以知道我在找什么 试图应用这些解决方案,但我想我遗漏了一些东西。这是我正在寻找的另一个例子 这是我的班级宣言: import UIKit import MapKit import CoreLocation import Parse class MapViewController: UIViewController, MKMapViewDe

关于Swift 1.2:我想在地图上的注释中添加一个按钮。首先点击pin显示用户的id,这正在工作,但是,通过按下按钮,我想将用户发送到详细视图控制器

你可以知道我在找什么

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

这是我的班级宣言:

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)
            }

        })

    }
//标记:-创建注释
func createAnnotations(点:PFGeoPoint,地址:字符串)
{
println(“***此值为n.3***”)
var query=PFUser.query()
查询?.whereKey(“位置”,近地点:点,带测高仪:带测高仪)
query?.orderByAscending(“位置”)//标记:按顺序排列列表
//标记:不包括地图上的当前用户
var me=PFUser.currentUser()?.username
查询?.whereKey(“用户名”,notEqualTo:me!)
query?.limit=self.kLimitNumberForQueryResults
查询?.FindObjectsInBackgroundithBlock({(对象,错误)->中的Void
如果错误==nil
{
对于(变量i=0;i

提前谢谢

使用“viewForAnnotation”地图视图委派方法设置左侧和右侧标注附件。此外,UIButton.buttonWithType被替换,这将起作用:编辑:您需要的所有委托方法,但我没有通过您的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

}

我试着把func放在我的“createAnnotations”上面,但得到了这个:MapViewController.swift:192:51:call中的额外参数“type”你在使用Swift1.2吗?该代码在swift2.0上运行良好。我建议更新:)Ps,您不应该将该函数放入函数中。这是一种委托方法是的,我会的,但现在我必须在1.2上关闭它:(然后我会切换,但把它放在一边,我把你的func添加到我的“createAnnotations”func的正上方,作为一个单独的func,对吗?