2010-08-15 76 views
1

想要解決一個我正面臨的mapkit問題。應該是一個愚蠢的問題,或者我在通過mapkit框架時錯過了一些東西。帶多註解的Mapkit(標註),映射下一個視圖

這裏是塞納里奧。 當用戶執行一些像披薩一樣的搜索時,我在地圖上放置了多個註釋。 添加了右側註釋視圖的按鈕,點擊後打開下一個詳細視圖。問題是如何將一些信息發送到下一個視圖,例如我在創建註釋時添加了註釋索引,現在我想從註釋中訪問這些信息,並通過按鈕上的選擇器將其傳遞到下一個視圖。

我已經檢查了所有mapkit微妙,但沒有找到一個我可以映射此信息與下一個視圖和註釋的地方。

希望我沒有在你的問題上困惑你們。請讓我知道我會重構它。

提前結案。

+0

我也創建註釋引腳作爲ü沒有。對我的註釋pi的點擊我需要通過一些值到我的下一堂課,但我陷入了困境。請你幫我或提供一些示例代碼,將點擊註釋上的值傳遞給下一個班級 – Rocky 2011-05-11 10:53:38

回答

9

在創建UIButton用於註解,設置tag屬性(標籤是一個UIViewNSInteger屬性),以標識相關的對象的ID或數組索引。然後,您可以從sender參數中將該標記值檢索到您的選擇器。


編輯:這裏的一些示例代碼。

您創建註釋視圖按鈕在你委託的-mapView關聯:viewForAnnotation:方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { 
    // Boilerplate pin annotation code 
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"]; 
    if (pin == nil) { 
     pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease]; 
    } else { 
     pin.annotation = annotation; 
    } 
    pin.pinColor = MKPinAnnotationColorRed 
    pin.canShowCallout = YES; 
    pin.animatesDrop = NO; 

    // now we'll add the right callout button 
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

    // customize this line to fit the structure of your code. basically 
    // you just need to find an integer value that matches your object in some way: 
    // its index in your array of MKAnnotation items, or an id of some sort, etc 
    // 
    // here I'll assume you have an annotation array that is a property of the current 
    // class and we just want to store the index of this annotation. 
    NSInteger annotationValue = [self.annotations indexOfObject:annotation]; 

    // set the tag property of the button to the index 
    detailButton.tag = annotationValue; 

    // tell the button what to do when it gets touched 
    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside]; 

    pin.rightCalloutAccessoryView = detailButton; 
    return pin; 

}在動作方法

然後,你會從tag解壓的價值和使用它來顯示右細節:

-(IBAction)showDetailView:(UIView*)sender { 
    // get the tag value from the sender 
    NSInteger selectedIndex = sender.tag; 
    MyAnnotationObject *selectedObject = [self.annotations objectAtIndex:selectedIndex]; 

    // now you know which detail view you want to show; the code that follows 
    // depends on the structure of your app, but probably looks like: 
    MyDetailViewController *detailView = [[MyDetailViewController alloc] initWithNibName...]; 
    detailView.detailObject = selectedObject; 

    [[self navigationController] pushViewController:detailView animated:YES]; 
    [detailView release]; 
} 
+0

可以將你的plz粘貼一個樣本,你的方法看起來讓我感到困惑 – Ameya 2010-08-15 17:53:35

+0

看到上面編輯。 – 2010-08-15 18:28:44

+0

糟糕,忘記將按鈕添加到引腳。 – 2010-08-15 22:09:16

1

在Annotation視圖,是有可能抓住,比方說,標題或副標題或任何其他創建引腳時使用的信息?我正在做的是在基於這些變量之一的註釋中彈出一個特定的圖像。

#import "MapPin.h" 

@implementation MapPin 


@synthesize coordinate; 
@synthesize title; 
@synthesize subtitle; 
@synthesize indexnumber; 
@synthesize imageFile; 

-(id)initWithCoordinates:(CLLocationCoordinate2D)location 
       placeName: placeName 
      description:description 
       indexnum:indexnum 
      imageFileLoc:imageFileLoc{ 

    self = [super init]; 
    if (self != nil) { 
     imageFile=imageFileLoc; 
     [imageFile retain]; 
     indexnumber=indexnum; 
     [indexnumber retain]; 
     coordinate = location; 
     title = placeName; 
     [title retain]; 
     subtitle = description; 
     [subtitle retain]; 
    } 
    return self; 

} 



-(void)addAnnotations { 

    // Normally read the data for these from the file system or a Web service 
    CLLocationCoordinate2D coordinate = {35.9077803, -79.0454936}; 
    MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate 
              placeName:@"Keenan Stadium" 
             description:@"Tar Heel Football" 
              indexnum:@"1" 
             imageFileLoc:@"owl.jpg"]; 
    [self.map addAnnotation:pin]; 
+0

請另附一個新的問題。 – 2011-04-26 08:51:58

1

另一種選擇:

您可以實現這些方法:

- (void)mapView:(MKMapView *)mapView didSelectAnnotation:(MKAnnotationView *)view; 
- (void)mapView:(MKMapView *)mapView didDeselectAnnotation:(MKAnnotationView *)view;