2012-07-18 64 views
-1

我有一個表格視圖,從數組中加載數據並顯示數據,每個單元指向相同的細節視圖重新填充。TableView和MapView相同的細節查看

在同一視圖中,我有一個隱藏的地圖視圖,地圖視圖顯示地圖中相同的表格數組,每個註解都有一個指向表的相同細節視圖的揭示按鈕,但我不'噸明白如何可以從地圖視圖的細節視圖傳遞數據...

用於本公開按鈕i使用此代碼

-(IBAction)showDetails:(id)sender{ 

DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:@"DettMercatiViewController" bundle:nil]; 
dettMercatiViewController.mieiMercati = [table objectAtIndex:????]; 
[self.navigationController pushViewController:dettMercatiViewController animated:YES]; 
[dettMercatiViewController release]; 

} 

表是從XML創建的NSMutableArray的名稱,但我不知道什麼objectIndex使用...任何想法?

OK,這裏的viewDidLoad中和引腳的創建,請幫我解釋一下,因爲我是孤獨:

- (void)viewDidLoad 
{ 
    pp =0; 

//Colore NavigationBar 
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:33.0f/255.0f green:89.0f/255.0f blue:50.0f/255.0f alpha:1.0f]; 

//Font Navigation Bar 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)]; 
label.backgroundColor = [UIColor clearColor]; 
label.font = [UIFont fontWithName:@"Museo700-Regular" size:20]; 
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
label.textAlignment = UITextAlignmentCenter; 
label.textColor =[UIColor whiteColor]; 
label.text=[self.title uppercaseString]; 
self.navigationItem.titleView = label; 
[label release]; 


//creazione nome XML 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *nomeXML = [NSString stringWithFormat: @"%@_%@_%@",[defaults stringForKey:@"interesse"],[defaults stringForKey:@"provincia"],[defaults stringForKey:@"giorno"]]; 

NSLog(@"%@", nomeXML); 

//Caricamento XML 
table = [[NSMutableArray alloc] init]; 
NSString *url = [[NSBundle mainBundle] pathForResource:nomeXML ofType:@"xml"]; 
XMLParser *myParser = [[[XMLParser alloc]autorelease] parseXMLAtURL:url toObject:@"Mercato" parseError:nil]; 

for(int i = 0; i < [[myParser items] count]; i++) { 
    Mercato *new = [[myParser items] objectAtIndex:i]; 
    [table addObject:new]; 





/////////////////////////////////////////// 
//definizione posizione utente 

AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate; 


CLLocation *userLoc = appDelegate.locationManager.location; 
CLLocationCoordinate2D userCoordinate = userLoc.coordinate; 

NSLog(@"user latitude = %f",userCoordinate.latitude); 
NSLog(@"user longitude = %f",userCoordinate.longitude); 

mapView.delegate=self; 


//Pin Mappa 


    CLLocationCoordinate2D theCoordinate; 
    theCoordinate.latitude = [new.latitudine doubleValue]; 
    theCoordinate.longitude = [new.longitudine doubleValue]; 

    myAnnotation=[[MyAnnotation alloc] init]; 


    myAnnotation.coordinate=theCoordinate; 
    myAnnotation.title=[NSString stringWithFormat:@"%@", new.ubicazione]; 
    myAnnotation.subtitle=[NSString stringWithFormat:@"%@, %@, %@",new.comune, new.giorno, new. orario]; 




    [mapView addAnnotation:myAnnotation]; 

    [annotations addObject:myAnnotation]; 


    if([new.latitudine doubleValue] == 0) { 
     [mapView removeAnnotation:myAnnotation]; 
     myAnnotation = nil; 
    } 


    // Inizializza mappa al Centro della Provincia 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"bergamo"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.6982642; 
     newRegion.center.longitude = 9.6772698; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"brescia"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.5411875; 
     newRegion.center.longitude = 10.2194437; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"como"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.8080597; 
     newRegion.center.longitude = 9.0851765; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"cremona"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.133249; 
     newRegion.center.longitude = 10.0226511; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"lecco"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.8565698; 
     newRegion.center.longitude = 9.3976704; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"lodi"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.3138041; 
     newRegion.center.longitude = 9.5018274; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"mantova"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.1564168; 
     newRegion.center.longitude = 10.7913751; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"milano"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.4654542; 
     newRegion.center.longitude = 9.186516; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.512872; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"monza e brianza"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.623599; 
     newRegion.center.longitude = 9.2588015; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"pavia"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.1847248; 
     newRegion.center.longitude = 9.1582069; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"sondrio"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 46.1698583; 
     newRegion.center.longitude = 9.8787674; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 

    if (([[defaults stringForKey:@"provincia"] isEqualToString:@"varese"])) { 
     MKCoordinateRegion newRegion; 
     newRegion.center.latitude = 45.8205989; 
     newRegion.center.longitude = 8.8250576; 
     newRegion.span.latitudeDelta = 0.512872; 
     newRegion.span.longitudeDelta = 0.509863; 
     [mapView setRegion:newRegion animated:YES]; 
    } 


} 
//////////////////////////// 

    [super viewDidLoad]; 

    } 


#pragma mark MKMapViewDelegate 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ 
NSLog(@"welcome into the map view annotation"); 
pp++; 

// if it's the user location, just return nil. 
if ([annotation isKindOfClass:[MKUserLocation class]]){ 

    return nil; 

} 

// try to dequeue an existing pin view first 
static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:AnnotationIdentifier]autorelease]; 
pinView.animatesDrop=YES; 
pinView.canShowCallout=YES; 
pinView.pinColor=MKPinAnnotationColorGreen; 
pinView.tag = pp; 

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 

[rightButton setTitle:myAnnotation.title forState:UIControlStateNormal]; 
[rightButton addTarget:self 
       action:@selector(showDetails:) 
     forControlEvents:UIControlEventTouchUpInside]; 
pinView.rightCalloutAccessoryView = rightButton; 

return pinView; 


} 

    //////////////// 
    -(IBAction)showDetails:(id)sender{ 

    NSLog(@"%i", pp); 

    DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:@"DettMercatiViewController" bundle:nil]; 
dettMercatiViewController.mieiMercati = [table objectAtIndex:[sender tag]]; 
[self.navigationController pushViewController:dettMercatiViewController animated:YES]; 
[dettMercatiViewController release]; 

} 
+0

我不推薦標籤方法。有一種更直接的方式來訪問被點擊的註釋。例如,請參閱[this](http://stackoverflow.com/questions/9462699/how-to-recognize-which-pin-was-tapped)和[this](http://stackoverflow.com/questions/9876042 /註解的細節-後細節披露按下)。 – Anna 2012-07-18 13:28:39

回答

0

我建議讓您的地圖每一個披露按鈕查看標籤,你可以使用這個標籤比 所以

[table objectAtIndex:[sender tag]]; 
+0

我如何給每個註釋添加不同的標籤? – Acunamatata 2012-07-18 13:32:14

+0

添加按鈕後,「myAddedButton.tag = someIntegerValue;」 – 2012-07-18 13:51:41

+0

但我不知道我在地圖上有多少針可以給出累進數字? – Acunamatata 2012-07-18 13:58:24