2012-07-18 62 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