检测按下哪个注释标注公开按钮

时间:2014-04-07 20:39:06

标签: ios objective-c json uiviewcontroller annotations

我正在尝试检测按下了哪个注释公开按钮,以便在DetailController中显示该位置的特定信息。这个信息在JSon中解析,有关如何检测选择哪个注释的任何建议,然后将正确的信息解析为DetailController?这是我的ViewController.m文件

#import "ViewController.h"
#import "DetailController.h"
#import "Annotation.h"

#import "City.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet DetailController *detailViewController;


@end
#define getDatalURL @"http://www.club-hop.com/apptest.php"

@implementation ViewController

@synthesize mapView,jsonArray,citiesArray;

/*-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"clubName"]){
    NSString * name= @"clubName";
    NSString * line= @"clubLine";

    DetailViewController *dv= [segue destinationViewController];
    dv.cName=name;
    dv.cLine=line;
}
}*/



- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveData];
self.detailViewController = [[DetailController alloc] init];
/* Zoom the map to current location.
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserInteractionEnabled:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];*/
City * cityObject;

// load external page into UIWebView
NSMutableArray * locations= [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;

for(int u=0; u<citiesArray.count;u++){
cityObject=[citiesArray objectAtIndex:u];

myAnn=[[Annotation alloc]init];
NSNumber *aLat= cityObject.Latitude;
NSNumber *aLon= cityObject.Longitude;

double lat = [aLat doubleValue];
double lon = [aLon doubleValue];

location.latitude= lat;
location.longitude=lon;
myAnn.coordinate = location;
myAnn.title=cityObject.clubName;
myAnn.subtitle=cityObject.cityName;
[locations addObject:myAnn];}

[self.mapView addAnnotations:locations];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



//class methods
-(void) retrieveData{
NSURL * url= [NSURL URLWithString:getDatalURL];
NSData * data= [NSData dataWithContentsOfURL:url];
jsonArray= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//setup cities array
citiesArray=[[NSMutableArray alloc]init];

for(int i=0; i<jsonArray.count;i++){
    NSString * cID= [[jsonArray objectAtIndex:i] objectForKey:@"id"];
    NSString * cName= [[jsonArray objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cCountry= [[jsonArray objectAtIndex:i] objectForKey:@"cityCountry"];
    NSString * cLine= [[jsonArray objectAtIndex:i] objectForKey:@"clubLine"];
    NSString * clName= [[jsonArray objectAtIndex:i] objectForKey:@"clubName"];
    NSNumber * cLatitude= [[jsonArray objectAtIndex:i] objectForKey:@"Latitude"];
    NSNumber * cLongitude= [[jsonArray objectAtIndex:i] objectForKey:@"Longitude"];

    [citiesArray addObject:[[City alloc]initWithCityName:cName andCityCountry:cCountry  andClubName:clName andClubLine:cLine andLatitude:cLatitude andLongitude:cLongitude andCityId:cID]];

}

}

#pragma mark - MKMapViewDelegate

// user tapped the disclosure button in the callout
//
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view   calloutAccessoryControlTapped:(UIControl *)control

{   

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                       bundle:nil];
 self.detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"Page2"];
 [self.navigationController pushViewController:self.detailViewController animated:YES];

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id  <MKAnnotation>)annotation
{

MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:@"pinView"];

if (!pinView)
{
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation  reuseIdentifier:@"pinView"] ;
    pinView.pinColor=MKPinAnnotationColorGreen;
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    UIButton * rightButton= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView=rightButton;
}
else{
    pinView.annotation=annotation;
}
return pinView;

    }


@end

1 个答案:

答案 0 :(得分:0)

CityAnnotationView课程添加detailViewController媒体资源 -

在detailViewController.h和AnnotationView.h中添加

@class City;

@property (strong,nonatomic) City *city;

viewDidLoad的ViewController.m文件中,用以下内容替换当前的'for'循环

for (int u=0; u<citiesArray.count;u++) {
    cityObject=[citiesArray objectAtIndex:u];

    myAnn=[[Annotation alloc]init];

    myAnn.city=cityObject;           // Store the city object on the annotation

    NSNumber *aLat= cityObject.Latitude;
    NSNumber *aLon= cityObject.Longitude;

    double lat = [aLat doubleValue];
    double lon = [aLon doubleValue];

    location.latitude= lat;
    location.longitude=lon;
    myAnn.coordinate = location;
    myAnn.title=cityObject.clubName;
    myAnn.subtitle=cityObject.cityName;

    [locations addObject:myAnn];
}

或者,您可以为注释创建一个新的initWithCity:city方法,然后调用它并让初始化设置所有其他注释属性。这将是ViewController.m

viewDidLoad方法中的新“for”循环
for (int u=0; u<citiesArray.count;u++) {
        cityObject=[citiesArray objectAtIndex:u];

        myAnn=[[Annotation alloc]initWithCity:cityObject];

        [locations addObject:myAnn];
    }

在annotation.m

#import "City.h"

-(id)initWithCity:(City *)city
{
        self=[super init];

        if (self) {
           self.city=city;           // Store the city object on the annotation

        NSNumber *aLat= city.Latitude;
        NSNumber *aLon= city.Longitude;

        double lat = [aLat doubleValue];
        double lon = [aLon doubleValue];

        CLLocationCoordinate2D location;

        location.latitude= lat;
        location.longitude=lon;
        self.coordinate = location;
        self.title=city.clubName;
        self.subtitle=city.cityName;

     }
     return (self);
}

无论哪种方式,ViewController.m中的calloutAccessoryControlTapped:变为 -

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view   calloutAccessoryControlTapped:(UIControl *)control

{   

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                       bundle:nil];
    self.detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"Page2"];

    Annotation *myAnnotation=(Annotation *)view.annotation;

    self.detailViewController.city=myAnnotation.city;

    [self.navigationController pushViewController:self.detailViewController animated:YES];

}