MKMapView显示DetailView – 如何使一个segue
我有一个MapView,注释来自一个属性列表。 现在我想在详细视图中看到更多的数据。 不幸的是,我不怎么编程Segue,以便显示数据。 我希望你明白我的意思。 我的英语不太好…我知道在prepareforSegue方法中缺less一些。 我正在使用故事板。 提前感谢您的帮助…问候
#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]; } }
当您执行performSegueWithIdentifier:sender
来自annotationView:view
的标注时,发件人可以是view.annotation
属性,它唯一标识用户刚刚点击的标注。
我相信你没有在Interface Builder中设置Segue的标识符。 我试过你的代码,它为我工作(Xcode 4.4的iOS 5.1 SDK)
以上两个答案有帮助,但是不能帮助您确定我假设的唯一引脚细节是您的p列表的一部分。
所有视图inheritance标签的属性。 它可以用来保存一个引脚索引(或键),因此将其传递给目标视图控制器来唯一标识所保存的数据。
由于您的调出视图中的button从视图inheritance,因此您可以对其进行标记,并在方法didselectAnnotationView中处于活动状态时传递索引。 if语句取消select用户位置引脚。
-(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现在可以用作selectsegue的参数。
-(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,但是主体是一样的。