【问题标题】:How can i add a disclosure button to a MKAnnotation?如何向 MKAnnotation 添加披露按钮?
【发布时间】:2012-12-26 22:22:35
【问题描述】:

我想在MKAnnotation 中添加一个披露按钮以转到另一个视图。

按钮应该如下所示:

Image

这是我的 .h.m 文件。


.h 文件

//
//  POI.h
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface POI : NSObject <MKAnnotation> {

    NSString *title;
    NSString *subtitle;
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)_coordinate title:(NSString *)_titolo andSubTitle:(NSString *)_sottotitolo;


@end

.m 文件

//
//  POI.m

#import "POI.h"



@implementation POI

@synthesize title, subtitle, coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D)_coordinate title:(NSString *)_titolo andSubTitle:(NSString *)_sottotitolo {

    [self setTitle:_titolo];
    [self setSubtitle:_sottotitolo];
    [self setCoordinate:_coordinate];



    return self;
}

@end

在我的 ViewController 中我使用:

  pinLocation.latitude = 4.8874;
    pinLocation.longitude = 1.400;
    POI *poi = [[POI alloc] initWithCoordinate:pinLocation title:@"foo" andSubTitle:@"bar"];
    [_mapView addAnnotation:poi];

【问题讨论】:

    标签: objective-c android-mapview mapkit mkannotation disclosure


    【解决方案1】:

    三个步骤。

    1) 在你的头文件 (.h) 你的实现文件 (.m) 的类扩展符合MKMapViewDelegate:

    @interface ViewController : UIViewController <MKMapViewDelegate> { ... } 
    

    2) 将您的视图控制器设置为MKMapViewDelegate 的委托,以接收委托回调。常见于viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.mapView.delegate = self;
    }
    

    3) 实现以下委托函数以显示披露按钮:

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
    {   
        MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];
    
        newAnnotation.canShowCallout = YES;
        newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    
        return newAnnotation;
    }
    

    以下功能将帮助确定在触摸披露按钮时采取的操作(在您的情况下,是呈现视图)。

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        //launch a new view upon touching the disclosure indicator
        TestVCViewController *tvc = [[TestVCViewController alloc] initWithNibName:@"TestVCViewController" bundle:nil];
        [self presentViewController:tvc animated:YES completion:nil];
    }
    

    【讨论】:

    • 感谢您的回复,我尝试添加您的代码,但没有显示任何内容...
    • 您目前是否绘制了任何点?您是否在视图控制器的头文件中声明了 的使用?您需要这样做才能访问 MapKit 委托方法。我会在我的回答中澄清这一点。
    • 是的,这是我的标题,谢谢! [代码] @interface LFViewController : UIViewController { BOOL _doneInitialZoom; CLLocationManager *locationManager; }[代码]
    • 好的,我解决了实现您的代码并添加 _mapView.delegate = self;在 - (void)viewDidLoad 中,非常感谢!
    • 如果您显示的是userLocation,请确保在if (annotation == mapView.userLocation) { 中返回具有不同标识符的MKPinAnnotationView
    【解决方案2】:

    在你的- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id&lt;MKAnnotation&gt;)annotation 方法中使用这个:

    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    

    annotationView 是要返回的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多