我正在建立一个iPhone应用程序,我的标签一直抓住我的plist中的最后一项。应用程序的第一部分显示带注释的地图。注释 - 纬度和经度以及标题都是从同一个plist中提取的。
这是我的代码:
#import "myMapViewController.h"
#import "MapViewAnnotation.h"
@interface myMapViewController ()
@property NSArray *states;
//@property NSString *nameString;
//@property NSString *zipString;
//@property NSString *subtitleString;
//@property NSString *zipString;
@end
@implementation myMapViewController
@synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.mapView.showsUserLocation = YES; //showsUserLocation = YES;
self.mapView.delegate = self;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated: YES];
[self.mapView addAnnotations:[self createAnnotations]];
//[self zoomToLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSMutableArray *)createAnnotations
{
NSMutableArray *annotations = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle]pathForResource:@"States" ofType:@"plist"];
NSArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
int count = rootLevel.count;
for (int i = 0; i < count; i++){
NSDictionary *secondLevel = [rootLevel objectAtIndex:i];
// NSArray *state = [secondLevel valueForKey:@"State"];
NSArray *stores = [secondLevel valueForKey:@"Stores"];
for (NSDictionary *row in stores) {
NSString *latitude = [row objectForKey:@"Latitude"];
NSString *longitude = [row objectForKey:@"Longitude"];
NSString *title = [row objectForKey:@"Name"];
NSString *nameString = [row objectForKey:@"Number"];
NSString *zipString = [row objectForKey:@"Zip"];
NSString *subtitle = [row objectForKey:@"Address1"];
//Create coordinates from the latitude and longitude values
CLLocationCoordinate2D coord;
coord.latitude = latitude.doubleValue;
coord.longitude = longitude.doubleValue;
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title AndCoordinate:coord AndZipString:zipString AndNameString:nameString AndSubSubtitle:subtitle];
[annotations addObject:annotation];
//set label
}
}
return annotations;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"showStoreDetails"])
{
MKAnnotationView *annotationView = sender;
id<MKAnnotation> ann = annotationView.annotation;
[segue.destinationViewController setNameString:ann.title];
[segue.destinationViewController setZipString:ann.title];
NSLog(@"log %@", annotationView.annotation.subtitle);
}
}
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id < MKAnnotation >)annotation
{
static NSString *reuseId = @"StandardPin";
MKPinAnnotationView *aView = (MKPinAnnotationView *)[sender
dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
if (aView == nil)
{
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseId];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.canShowCallout = YES;
//aView.animatesDrop = YES;
}
aView.annotation = annotation;
return aView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"showStoreDetails" sender:view];
NSLog(@"accessory button tapped for annotation %@", view.annotation.title);
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.7;
span.longitudeDelta=0.7;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
@end
答案 0 :(得分:1)
当您在plist中进行迭代时,需要一个数组来存储namestring
而不是简单的self.namestring
NSString。
答案 1 :(得分:1)
看起来你真的没有给MapViewAnnotation
对象提供该位置的标签。
当您从plist中循环数据时,标签上发生的唯一事情是它被设置为self.nameString
,这将被循环的每次迭代一直覆盖。
不确定您的代码究竟在尝试做什么,但看起来您在name
值上做得不够,无法实现您的目标。
答案 2 :(得分:0)
这很简单。你的内部循环完成了为标签分配文本的工作。这两个循环遍历所有记录并继续将名称分配给nameString,删除第一个记录的标签
self.nameString = nameString;
因此仅存储最后一个字符串。
希望这有帮助。
谢谢, KUNAL
答案 3 :(得分:0)
问题似乎是在prepareForSegue
中,您正在向self.nameString
发送destinationViewController
,但该变量仅包含最后读取的项目。
请注意,在calloutAccessoryControlTapped
中,您使用performSegueWithIdentifier
作为sender
来调用view
(即所选注释的视图) ,但在prepareForSegue
中,您没有使用sender
。
您应该在sender
而不是prepareForSegue
中使用self.nameString
。
有关详细示例,请参阅MKAnnotationView Push to View Controller when DetailDesclosure Button is Clicked。
prepareForSegue
中的更新代码可能如下所示:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"showStoreDetails"])
{
MKAnnotationView *annotationView = sender;
id<MKAnnotation> ann = annotationView.annotation;
[segue.destinationViewController setNameString:ann.title];
}
}