热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在MKPointAnnotation中添加一个按钮?-HowtoaddabuttontotheMKPointAnnotation?

Ivejustgotstucktryingtoaddadetailbuttontomyannotationpoint,unfortunatelyIdontknow

I've just got stuck trying to add a detail button to my annotation point, unfortunately I don't know how to do it. Does anyone could help me with that?

我刚在注释点添加了一个细节按钮,不幸的是我不知道怎么做。有人能帮我吗?

The image below presents what I'd like to achieve. Thanks! enter image description here

下面的图片展示了我想要达到的效果。谢谢!

MapKitViewController:

MapKitViewController:

import UIKit
import MapKit
import CoreLocation

class MapKitViewController: UIViewController, MKMapViewDelegate
{

let locatiOnManager= CLLocationManager()

@IBOutlet weak var nmapView: MKMapView!
override func viewDidLoad()
{
    super.viewDidLoad()
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    let location = CLLocationCoordinate2D(
        latitude: 53.4265107,
        longitude: 14.5520357)

    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    nmapView.setRegion(region, animated: true)
    nmapView.showsPointsOfInterest = false
    nmapView.showsUserLocation = true
    displayMarkers()
}

func displayMarkers() -> Void
{
    let jsonURL: NSURL = NSURL(string: "http://jsonstring.com/")!

    var dataFromNetwork: NSData = NSData(contentsOfURL: jsonURL)!
    let json = JSON(data: dataFromNetwork)
    var jsOnSize= json.count

    var todaysDate:NSDate = NSDate()
    var dateFormatter:NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    var formattedDate:String = dateFormatter.stringFromDate(todaysDate)

    let annotatiOnView= MKAnnotationView()
    let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
    annotationView.rightCalloutAccessoryView = detailButton


    for(var i = 0; i 

1 个解决方案

#1


10  

You are doing it right.You just need to have these methods implemented for adding button along with title and subtitle

你做得对。您只需要实现这些方法来添加按钮以及标题和副标题

iOS 8 and Xcode 6

iOS 8和Xcode 6

 import UIKit
 import MapKit
 import CoreLocation

 class MapKitViewController: UIViewController, MKMapViewDelegate
 {
    let locatiOnManager= CLLocationManager()
    @IBOutlet weak var nmapView: MKMapView!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        let location = CLLocationCoordinate2D(
            latitude: 53.4265107,
            longitude: 14.5520357)

        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: location, span: span)
        nmapView.setRegion(region, animated: true)
        nmapView.showsPointsOfInterest = false
        nmapView.showsUserLocation = true
        displayMarkers()
    }

    // When user taps on the disclosure button you can perform a segue to navigate to another view controller
    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if cOntrol== view.rightCalloutAccessoryView{
            println(view.annotation.title) // annotation's title
            println(view.annotation.subtitle) // annotation's subttitle

            //Perform a segue here to navigate to another viewcontroller
            // On tapping the disclosure button you will get here
        }
    }

    // Here we add disclosure button inside annotation window
    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        println("viewForannotation")
        if annotation is MKUserLocation {
            //return nil 
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {
            //println("Pinview was nil")
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            }

        var button = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton // button with info sign in it

        pinView?.rightCalloutAccessoryView = button


        return pinView
    }


    func displayMarkers() -> Void
    {
        let jsonURL: NSURL = NSURL(string: "http://atnight.wtznc.com/json.php")!

        var dataFromNetwork: NSData = NSData(contentsOfURL: jsonURL)!
        let json = JSON(data: dataFromNetwork)
        var jsOnSize= json.count

        var todaysDate:NSDate = NSDate()
        var dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        var formattedDate:String = dateFormatter.stringFromDate(todaysDate)

        let annotatiOnView= MKAnnotationView()

        // Adding button here wont do anything  so remove these two lines


        let detailButton: UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
        annotationView.rightCalloutAccessoryView = detailButton

        // For adding button we have to use a method named as viewForAnnotation 

        for(var i = 0; i 

Check out my output.

看看我的输出。

enter image description here


推荐阅读
author-avatar
Hancl
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有