2013-09-05 31 views
0

作爲標題描述我有麻煩顯示數據,我有我的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於註釋和細節視圖是進口的。唯一沒有顯示的東西是細節視圖中的字符串和圖像。

回答

1

你可以使用

myAnnotation.annotationData = [anns objectAtIndex:i]; 

然後在prepareForSegue:sender:,你可以訪問標記數據,並將其傳遞到詳細視圖控制器通過特定的數據對象的Annotation


編輯:添加示例代碼

聲明的註解類的屬性:

@property (nonatomic, strong) NSDictionary *annotationData; 

聲明相同的視圖控制器上的屬性爲您的地圖視圖:

@property (nonatomic, strong) NSDictionary *selectedAnnotationData; 

並執行map view delegate meth外徑:基於編輯的問題:

-(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:@"MY_SEGUE_IDENTIFIER" sender:self]; 
} 

然後,註釋數據傳遞到在prepareForSegue隨後控制器:發送方:方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showPlattegrondDetails"]) 
    { 
     ItalyMapDetail *controller = segue.destinationViewController; 
     controller.annotationData = self.selectedAnnotationData; 
    } 
} 

編輯2。

不知道plist中鍵的名字,我只能指出你應該看的方向。

ItalyMapDetail控制器應具有如前面控制器prepareForSegue:sender:提供的註釋數據字典,它應該只使用從字典中的值來填充你的UI的情況下...

更新您的viewDidLoad的方法ItalyMapDetail控制器如下:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // temporary test: print out the annotation data so 
    // we can see what we've got. This shouldn't be nil. 
    NSLog("Annotation Data: %@", self.annotationData); 

    // replace these keys with appropriate ones for your plist 
    appImage.image = [UIImage imageNamed:self.annotationData[@"imageName"]]; 
    appText.text = self.annotationData[@"appText"]; 
    appName.text = self.annotationData[@"appName"]; 
} 
+0

您需要在'Annotation'類中聲明一個名爲'annotationData'的NSDictionary @property。對不起,我沒有在我的示例中包括這個:( –

+0

我的答案的原始版本建議您將當前註釋的整個字典傳遞到註釋中。在你設置每個註釋的for循環中完成,如問題的最終代碼片段所示。 –

+0

你可以使用最新嘗試的更多代碼更新你的問題嗎?我正在努力查看問題出在哪裏。 –

相關問題