单击地图注释时显示另一个视图

发布于 2024-12-03 03:45:32 字数 1381 浏览 1 评论 0原文

我有一张只有一个注释的地图。我创建了一个简单的类,我希望它在用户单击注释时显示。问题是,当我单击注释时什么也没有发生。

这是我的代码:

- (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 buttonWithType: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];
}

我的 showDetailView 函数永远不会被调用。请帮助我。我是 iPhone 新手,我可能会忘记一件简单的事情。谢谢

仍然无法正常工作!!!!

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 buttonWithType: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!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愿与i 2024-12-10 03:45:32

首先,检查地图视图的 delegate 是否已设置,否则您的 viewForAnnotation 方法将不会被调用。

接下来,附件按钮出现在注释的callout上,只有在设置canShowCallout时才会出现:

annView.canShowCallout = YES;

接下来,不要使用自己的方法来处理按钮操作,这样会更好使用地图视图自己的 calloutAccessoryControlTapped 委托方法,该方法在点击附件时被调用:

- (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
}

从以下位置删除 [detailButton addTarget...viewForAnnotation

另请注意,showDetailView 方法中的 NSLog 缺少前导 @,这将导致调用该方法时崩溃。

另一件事是您应该在 viewForAnnotation 中使用 dequeueReusableAnnotationViewWithIdentifier 来启用注释视图的重用。

请求示例

- (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];
}

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];
}
末蓝 2024-12-10 03:45:32

好的。如果您使用故事板,则需要在地图视图控制器和以下代码所在的位置之间创建一个 Segue:

- (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];
}

请改用以下代码:

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

这将使您移动到详细视图。

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.

心如荒岛 2024-12-10 03:45:32

像这样编写你的函数:

-(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];
}

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];
}
伴梦长久 2024-12-10 03:45:32

您必须将地图委托设置为 self,如下所示,

mapview.delegate=self;(如果您在同一页面中调用该方法),

如果您不在代码中分配委托,则相应的方法将获胜不会被调用,因此请检查您是否有上述线路。

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.

把回忆走一遍 2024-12-10 03:45:32

-(IBAction)showDetailView:(UIView*)sender 替换为以下...

    -(void)showDetailView:(id)sender

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

    -(void)showDetailView:(id)sender
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文