2012-07-31 72 views
0

我有一個MapView,註釋來自屬性列表。 現在我想在詳細視圖中看到更多數據。 不幸的是,我不怎麼編程Segue,以便顯示數據。 我希望你明白我的意思。我的英語不太好... 我知道在prepareforSegue方法中缺少一些。 我正在使用故事板。 在提前感謝你的幫助...... 問候MKMapView顯示DetailView - 如何使一個segue

#import "MapViewNewController.h" 
#import "MapViewAnnotation.h" 
#import "SetCardController.h" 



@interface MapViewNewController() 



@end 

@implementation MapViewNewController 


@synthesize recipesDictionary = _recipesDictionary; 

@synthesize mapView; 
@synthesize locationManager; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.mapView.delegate = self; 

    [self.mapView setShowsUserLocation:YES]; 


    MKCoordinateRegion newRegion ; 
    newRegion.center.latitude = 50.080635; 
    newRegion.center.longitude = 8.518717; 
    newRegion.span.latitudeDelta = 15.5; 
    newRegion.span.longitudeDelta = 15.5; 
    [mapView setRegion:newRegion animated:YES]; 



    NSString *path = [[NSBundle mainBundle] pathForResource:@"Rezepte" ofType:@"plist"]; 
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
    NSArray *anns = [dict objectForKey:@"myRecipes"]; 
    for(int i = 0; i < [anns count]; i++) { 
     float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"latitude"] floatValue]; 
     float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"longitude"] floatValue]; 

     MapViewAnnotation *myAnnotation = [[MapViewAnnotation alloc] init]; 
     CLLocationCoordinate2D theCoordinate; 
     theCoordinate.latitude = realLatitude; 
     theCoordinate.longitude = realLongitude; 
     myAnnotation.coordinate = theCoordinate; 
     myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"blogname"]; 
     myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"blogger"]; 
     [mapView addAnnotation:myAnnotation]; 


    }  

} 


-(MKAnnotationView *)mapView:(MKMapView *)view viewForAnnotation:(id<MKAnnotation>)annotation { 
    MKPinAnnotationView *pin = nil; 

    if ([annotation isKindOfClass:[MapViewAnnotation class]]) { 
     NSString *pinIdentifier = @"myPin"; 


     pin = (MKPinAnnotationView*)[view dequeueReusableAnnotationViewWithIdentifier:pinIdentifier]; 
     if (!pin) { 


      pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier]; 

      pin.image = [UIImage imageNamed:@"pin2.png"]; 
      pin.canShowCallout = YES; 

      pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 


     } 
      } 

    return pin; 
} 



//if Button pressed 
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{ 

    [self performSegueWithIdentifier:@"showPinDetails" sender:self]; 




    } 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([segue.identifier isEqualToString:@"showPinDetails"]) { 

     // SetCardController *scc = [segue destinationViewController]; 


    } 
} 

回答

0

我相信你並沒有設置在Interface Builder中Segue公司的標識。我想你的代碼和它的工作對我(的Xcode 4.4的iOS 5.1 SDK)

1

當你performSegueWithIdentifier:sender從裏面annotationView:view標註,發送方可以在view.annotation屬性,該屬性唯一標識,用戶只需輕敲標註。

0

以上兩個答案有幫助,但是不幫助您確定我假設的唯一引腳細節是您的p列表的一部分。

所有視圖繼承標籤的屬性。它可以用它來保存一個引腳索引(或鍵),因此可以將它傳遞給目標視圖控制器來唯一標識你所保存的數據。

由於您在調出視圖中的按鈕從視圖中繼承,因此您可以對其進行標記,並在其方法didselectAnnotationView中處於​​活動狀態時傳遞索引。所使用的if語句取消選擇用戶位置引腳。

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 

if (![(PointAnnotation *)view.annotation isKindOfClass:[MKUserLocation class]]) 
{ 
    PointAnnotation *myAnn = (PointAnnotation *)view.annotation; 

    detailButton.tag = myAnn.pinIndex; 
    NSLog (@"Pinindex = %d", myAnn.pinIndex); 
} 



} 

detailButton.tag現在可以作爲選擇segue的參數。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
// Post data to destination VC 

if ([[segue identifier] isEqualToString:@"clubsToDetail"]) 
{ 


    ClubsMasterData *current = [[ClubsMasterxmlParser ClubsMasterDatas] objectAtIndex:detailButton.tag]; 


    ClubsDetailViewController *DVC = [[ClubsDetailViewController alloc] init]; 
    DVC = [segue destinationViewController]; 


    DVC.linkURL = current.linkURL; 
    DVC.distance = current.distance; 

} 
} 

在這裏,我索引了一個數組而不是Plist,但主體是相同的。