如何在MKAnnotation中发送对象?

时间:2012-05-31 08:28:16

标签: objective-c xcode mkannotation

我想在用户点击引脚时发送对象。我只知道如何发送标题,但这还不够,因为有些标题是相同的。

这是我的代码:

...
- (void)viewDidLoad {
    [super viewDidLoad];

    if (context == nil) 
    { 
        context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    }


    /* ********************************************** */
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Offers" inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];

//[request setFetchBatchSize:20];

[request setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subOffer == %@", self.subItem];
[request setPredicate:predicate];


NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"offerOrder" ascending:YES];

NSArray *newArray = [NSArray arrayWithObject:sort];

[request setSortDescriptors:newArray];

NSError *error;

NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];

//NSLog(@"%@", results);


/* **************************************************** */

    mapView.delegate=self;

    NSMutableArray* annotations=[[NSMutableArray alloc] init];


    CLLocationCoordinate2D theCoordinate1;
    theCoordinate1.latitude = 43.82078;
    theCoordinate1.longitude = 15.307265;



    for (Offers *offer in results){

        CLLocationCoordinate2D theCoordinate1;

        MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];

        theCoordinate1.latitude = [offer.lat doubleValue];
        theCoordinate1.longitude = [offer.lon doubleValue];

        myAnnotation1.coordinate=theCoordinate1;
        myAnnotation1.title = offer.offerName;
        myAnnotation1.subtitle = offer.offerLocation;


        [mapView addAnnotation:myAnnotation1];
        [annotations addObject:myAnnotation1];



    }//end for



    //NSLog(@"%d",[annotations count]);


    //[self gotoLocation];//to catch perticular area on screen
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    // Walk the list of overlays and annotations and create a MKMapRect that
    // bounds all of them and store it into flyTo.
    MKMapRect flyTo = MKMapRectNull;
    for (id <MKAnnotation> annotation in annotations) {

        //NSLog(@"fly to on");

        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) {
            flyTo = pointRect;
        } else {
            flyTo = MKMapRectUnion(flyTo, pointRect);
            //NSLog(@"else-%@",annotationPoint.x);
            //NSLog(@"else");
        }
    }





    // Position the map so that all overlays and annotations are visible on screen.
    mapView.visibleMapRect = flyTo;




    UIBarButtonItem* temp=[[UIBarButtonItem alloc] init];
    temp.title=@"Back";
    self.navigationItem.backBarButtonItem=temp;
    //[temp release];
}


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


    // 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";
    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    pinView.pinColor=MKPinAnnotationColorRed;


    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;


    return pinView;
}

-(IBAction)showDetails:(id)sender{

    //NSLog(@"Annotation Click");


    DetailViewController *detail = [[DetailViewController alloc] init];

    detail.title = ((UIButton*)sender).currentTitle;

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



}
...

0 个答案:

没有答案