单击地图注释时显示另一个视图
我有一张只有一个注释的地图。我创建了一个简单的类,我希望它在用户单击注释时显示。问题是,当我单击注释时什么也没有发生。
这是我的代码:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,检查地图视图的
delegate
是否已设置,否则您的viewForAnnotation
方法将不会被调用。接下来,附件按钮出现在注释的callout上,只有在设置
canShowCallout
时才会出现:接下来,不要使用自己的方法来处理按钮操作,这样会更好使用地图视图自己的
calloutAccessoryControlTapped
委托方法,该方法在点击附件时被调用:从以下位置删除
[detailButton addTarget...
行viewForAnnotation
。另请注意,showDetailView 方法中的
NSLog
缺少前导@
,这将导致调用该方法时崩溃。另一件事是您应该在
viewForAnnotation
中使用dequeueReusableAnnotationViewWithIdentifier
来启用注释视图的重用。请求示例:
First, check that the map view's
delegate
is set otherwise yourviewForAnnotation
method will not get called.Next, the accessory button appears on the annotation's callout which will only appear if you set
canShowCallout
: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:Remove the
[detailButton addTarget...
line fromviewForAnnotation
.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
inviewForAnnotation
to enable annotation view re-use.Example as requested:
好的。如果您使用故事板,则需要在地图视图控制器和以下代码所在的位置之间创建一个 Segue:
请改用以下代码:
这将使您移动到详细视图。
Ok. If you are using storyboards, you need to create a segue between the map view controller and then where the below code exists:
Use the following code instead:
This will give you the movement to the detailed view.
像这样编写你的函数:
write your function like this:
您必须将地图委托设置为 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.
将
-(IBAction)showDetailView:(UIView*)sender
替换为以下...Replace
-(IBAction)showDetailView:(UIView*)sender
with following...