MKMapView显示DetailView - 如何使一个segue

问题描述:

我有一个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]; 


    } 
} 

我相信你并没有设置在Interface Builder中Segue公司的标识。我想你的代码和它的工作对我(的Xcode 4.4的iOS 5.1 SDK)

当你performSegueWithIdentifier:sender从里面annotationView:view标注,发送方可以在view.annotation属性,该属性唯一标识,用户只需轻敲标注。

以上两个答案有帮助,但是不帮助您确定我假设的唯一引脚细节是您的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,但主体是相同的。