作爲標題描述我有麻煩顯示數據,我有我的plist註釋在地圖視圖項目我正在使用segue在故事板。我有地圖視圖,所有註釋(引腳)都正確顯示其座標。現在我想在用戶按下細節泄露按鈕時使用segue轉換爲詳細視圖。到目前爲止我有這是一個空的以下執行SEGUE:如何顯示數據從plist詳細視圖使用segue時按地圖視圖註釋
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"Details" sender:view];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
}
我想詳細視圖中顯示的細節被聲明爲字符串。如下
@interface ItalyMapDetail : UIViewController{
IBOutlet UIImageView *appImage;
IBOutlet UITextView *appText;
IBOutlet UILabel *appName;
NSString *appImageString;
NSString *appTextString;
NSString *appNameString;
}
@property (nonatomic, retain) NSString *appImageString;
@property (nonatomic, retain) NSString *appTextString;
@property (nonatomic, retain) NSString *appNameString;
@property (nonatomic, strong) UIImage* image;
@end
和.m文件是如下
@synthesize appImageString, appTextString, appNameString;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
appImage.image = [UIImage imageNamed:appImageString];
appText.text = appTextString;
appName.text = appNameString;
}
這是真正的詳細視圖某些字符串的一般聲明。由於我已經有一個導航控制器,因此segue標識符會觸發並且視圖會轉換爲詳細視圖並正確返回。現在我知道如何聲明字符串來準備segue,但是我不知道如何從segary中的plist聲明這些細節,以便在詳細視圖控制器中顯示註釋詳細信息。任何人都可以幫我一個這個。
哦,順便說一下,這裏是我從plist中導出的註釋的刪除,在我看來確實會加載,如果有幫助的話。
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Map" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"Italy"];
for(int i = 0; i < [anns count]; i++) {
float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];
Annotation *myAnnotation = [[Annotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
[myMapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
}
我知道這可能是大多數的你們很容易在那裏,但我確實被卡死,我可以使用代碼級的一些幫助。
編輯1:
這裏是變化後的代碼提示:
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"ItalyMap" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"Italy"];
for(int i = 0; i < [anns count]; i++) {
float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue];
float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue];
Annotation *myAnnotation = [[Annotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"];
myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"];
[myMapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
myAnnotation.annotationData = [anns objectAtIndex:i];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(@"view for annotation works");
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *annotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!pinView)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
[pinView setPinColor:MKPinAnnotationColorGreen];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"acolon1.png"]];
pinView.leftCalloutAccessoryView = iconView;
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
// Keep a reference to the callout's annotation data
self.selectedAnnotationData = [(Annotation *)view.annotation annotationData];
[self performSegueWithIdentifier:@"Details" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Details"])
{
ItalyMapDetail *controller = segue.destinationViewController;
controller.annotationData = self.selectedAnnotationData;
}
}
現在我已經加入DIC於註釋和細節視圖是進口的。唯一沒有顯示的東西是細節視圖中的字符串和圖像。
您需要在'Annotation'類中聲明一個名爲'annotationData'的NSDictionary @property。對不起,我沒有在我的示例中包括這個:( –
我的答案的原始版本建議您將當前註釋的整個字典傳遞到註釋中。在你設置每個註釋的for循環中完成,如問題的最終代碼片段所示。 –
你可以使用最新嘗試的更多代碼更新你的問題嗎?我正在努力查看問題出在哪裏。 –