在目标c中将id添加到pin注释以加载detailview [英] add id to pin annotation in objective c to load detailview

查看:86
本文介绍了在目标c中将id添加到pin注释以加载detailview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mapview控制器,该控制器显示了我创建的一系列业务对象中的图钉(每个公司都有一种获取图钉标题和副标题的方法).

I have a mapview controller which shows pins from an array of business objects I have created (each business has a method to get the title and subtitle of the pin).

我向每个引脚注释添加了一个显示按钮,该按钮可以正常工作,但是我不确定如何将变量传递到要从显示按钮加载的详细信息视图中,并显示该特定业务的所有详细信息

I have added a disclosure button to each pin annotation which works correctly, but I am not sure how to pass a variable to the detail view which is to be loaded from the disclosure button, and show all the details for that particular business.

我这样将我的公司添加到数组中(在viewWillAppear中)...

I add my businesses to the array like this (in viewWillAppear)...

// fetch model data for table view
SGTGAppDelegate *appDelegate = (SGTGAppDelegate *)[[UIApplication sharedApplication] delegate];

self.businesses = appDelegate.vaBusinesses;

// Add the business to the map
[self.mapView addAnnotations:self.businesses];

然后我像这样格式化注释...

I then format the annotations like this...

-(MKAnnotationView *)mapView:(MKMapView *)amapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *PinIdentifier = @"PinIdentifier";

    //Use default style for user location
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //Obtain a pin
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [amapView dequeueReusableAnnotationViewWithIdentifier:PinIdentifier];

    if (pin == nil){
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:PinIdentifier] autorelease];
    }

    UIButton * detailView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


    // Configue the pin
    pin.annotation = annotation;
    pin.animatesDrop = NO;
    pin.pinColor = MKPinAnnotationColorRed;
    pin.rightCalloutAccessoryView = detailView;
    pin.canShowCallout = YES;

    return pin;
}

然后,我有这种方法来处理披露按钮,但不确定在这里做什么使业务ID传递到详细信息视图中...

I then have this method to handle the disclosure button but not sure what do here to get an id of the business to pass onto the detail view...

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"annotation %@", view.annotation[0]);
    // Fetch the businesses for this row

    // not sure what to do here
    //SGTGVABusiness *business = [self.businesses objectAtIndex:[view.annotation]];

    // Show the detail view by pushing it onto the navigation stack
    SGTGVADetailViewController *dvc = [[SGTGVADetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
    //dvc.business = business;
    [self.navigationController pushViewController:dvc animated:YES];
    [dvc release];

}

推荐答案

此处真正需要自定义的是注释.从注释视图转到注释没有任何问题;问题是注释没有信息.您要做的是创建自己的注释类,这是一个实现MKAnnotation协议的NSObject子类,如下所示:

What really needs customization here is the annotation. You're not having any trouble getting from the annotation view to the annotation; the problem is that the annotation is uninformative. What you want to do is create your own annotation class, an NSObject subclass that implements the MKAnnotation protocol, like this:

@interface MyAnnotation : NSObject <MKAnnotation>
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title, *subtitle;
- (id)initWithLocation:(CLLocationCoordinate2D)coord;
@end

@implementation MyAnnotation
- (id)initWithLocation: (CLLocationCoordinate2D) coord {
    self = [super init];
    if (self) {
        self->_coordinate = coord;
    }
    return self;
}
@end

这是 minimum ,但是现在您可以对其进行扩展.特别是,您可以添加另一个属性,该属性存储有关此批注的额外信息.创建注释并将其添加到地图时,您将创建此类的实例,并为其分配信息,稍后您将需要获取这些信息.

That's minimal, but now you can expand on it. In particular, you can add another property that stores extra info about this annotation. When you create the annotation and add it to the map, you create an instance of this class and assign it the info you will need to fetch later.

我的书对此进行了深入讨论:

My book discusses this in depth:

http://www.apeth.com/iOSBook/ch34.html#_annotations

您可以下载一个开发此概念的工作项目:

And you can download a working project that develops this notion:

https://github.com/mattneub/编程-iOS-Book-Examples/tree/master/ch34p848map/p707p723map

这篇关于在目标c中将id添加到pin注释以加载detailview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆