开发者

show another view when map annotation are clicked

I have a map with only one annotation.I created a simple class which I want it to show when the user clicks on the annotation.The problem is that when I click on the annotation nothing happens.

Here is my code:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    NSLog(@"Reverse Geocoder completed");
    mPlacemark=placemark;
    [mapView addAnnotation:placemark];
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.animatesDrop=TRUE;

    //create UIButton for annotation
    UIButton *detailButton = [UIButton buttonWithTy开发者_如何学Gope:UIButtonTypeDetailDisclosure];
    //NSInteger annotationValue = [self.annotations indexOfObject:annotation];

    [detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView=detailButton;
    return annView;
}


-(void)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

My showDetailView function never gets called.Please help me.I'm new to iphone and I might forget a simple thing.Thanks

Still not working!!!!


First, check that the map view's delegate is set otherwise your viewForAnnotation method will not get called.

Next, the accessory button appears on the annotation's callout which will only appear if you set canShowCallout:

annView.canShowCallout = YES;

Next, instead of using your own method to handle the button action, it's much better to use the map view's own calloutAccessoryControlTapped delegate method which gets called when tapping an accessory:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"inside the stupid method");

    //Here, the annotation tapped can be accessed using view.annotation
}

Remove the [detailButton addTarget... line from viewForAnnotation.

Also note your NSLog in the showDetailView method is missing the leading @ which will result in a crash when the method does get called.

Another thing is you should use dequeueReusableAnnotationViewWithIdentifier in viewForAnnotation to enable annotation view re-use.


Example as requested:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    static NSString *annReuseId = @"currentloc";

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annReuseId];
    if (annView == nil)
    {
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annReuseId];

        annView.animatesDrop = YES;
        annView.canShowCallout = YES;

        UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annView.rightCalloutAccessoryView=detailButton;
    }
    else {
        annView.annotation = annotation;
    }

    return annView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}


Ok. If you are using storyboards, you need to create a segue between the map view controller and then where the below code exists:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}

Use the following code instead:

NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
[self performSegueWithIdentifier:@"DetailViewController" sender:self];

This will give you the movement to the detailed view.


write your function like this:

-(IBAction)showDetailView:(id)sender{
    NSLog("inside the stupid method");
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}


you have to set the map delegate to self like this,

mapview.delegate=self; (if your calling the method in same page)

if you don't assign the delegate in your code the respective methods won't be called, so check if you have the above mentioned line.


Replace -(IBAction)showDetailView:(UIView*)sender with following...

    -(void)showDetailView:(id)sender
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜