1

作成したビジネス オブジェクトの配列からピンを表示する mapview コントローラーがあります (各ビジネスには、ピンのタイトルとサブタイトルを取得するメソッドがあります)。

正しく機能する各ピン アノテーションに開示ボタンを追加しましたが、開示ボタンから読み込まれる詳細ビューに変数を渡し、その特定のビジネスのすべての詳細を表示する方法がわかりません。

私は自分のビジネスをこのように配列に追加します (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];

次に、注釈を次のようにフォーマットします...

-(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 を取得するためにここで何をすべきかわかりません...

-(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];

}
4

2 に答える 2

3

ここで本当にカスタマイズが必要なのは注釈です。注釈ビューから注釈に移動するのに問題はありません。問題は、注釈が有益でないことです。やりたいことは、次のように、MKAnnotation プロトコルを実装する NSObject サブクラスである独自の注釈クラスを作成することです。

@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

これは最小限ですが、これで拡張できます。特に、この注釈に関する追加情報を格納する別のプロパティを追加できます。注釈を作成してマップに追加するときは、このクラスのインスタンスを作成し、後で取得する必要がある情報を割り当てます。

私の本では、これについて詳しく説明しています。

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

そして、この概念を開発する作業プロジェクトをダウンロードできます。

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

于 2013-03-21T17:33:15.200 に答える
0

MKAnnotationView をサブクラス化し、ID を保持するプロパティを作成できます。

于 2013-03-21T17:17:45.163 に答える