Mapkit具有多个注释(callout),映射下一个视图 [英] Mapkit with multi annotation (callout), mapping the next view
问题描述
想要解决我面临的mapkit问题。应该是一个愚蠢的问题,或者我在浏览mapkit框架时错过了一些东西。
这是senario。
当用户执行像披萨这样的搜索时,我在地图上放置了多个注释。
添加了右侧注释视图的按钮,单击该按钮可打开下一个详细视图。问题是如何将一些信息发送到下一个视图,例如我在创建注释时添加索引,现在我想从注释中访问此信息,并通过按钮上的选择器将其传递给下一个视图。 / p>
我已经检查了所有mapkit,但找不到我可以用下一个视图和注释映射此信息的地址。
希望我没有在我的问题中困惑你们。请让我知道我会重新构建它。
提前预售。
为注释创建 UIButton
时,请设置标记
属性(标记为 NSInteger
UIView
的属性)到标识相关对象的id或数组索引。然后,您可以从 sender
参数中将该标记值检索到选择器。
编辑:这是一些示例代码。
您可以创建注释视图并将该按钮关联到代理的-mapView:viewForAnnotation:方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id< MKAnnotation>)注释{
// Boilerplate pin注释码
MKPinAnnotationView * pin =(MKPinAnnotationView *)[self.map dequeueReusableAnnotationViewWithIdentifier:@restMap];
if(pin == nil){
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@restMap] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed
pin.canShowCallout = YES;
pin.animatesDrop = NO;
//现在我们将添加正确的标注按钮
UIButton * detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//自定义此行以适合您的代码结构。基本上
//你只需要找到一个与你的对象在某种程度上匹配的整数值:
//它在你的MKAnnotation项数组中的索引,或某种类型的id等等
//
//这里我假设你有一个注释数组,它是当前
//类的一个属性,我们只想存储这个注释的索引。
NSInteger annotationValue = [self.annotations indexOfObject:annotation];
//将按钮的tag属性设置为索引
detailButton.tag = annotationValue;
//告诉按钮触摸时该怎么做
[detailButton addTarget:self action:@selector(showDetailView :) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = detailButton;
返回引脚;
}
然后在你的行动方法中,你将从标签
中解压缩价值并使用它来显示正确的细节:
- (IBAction)showDetailView:(UIView *)sender {
//从发件人获取标签值
NSInteger selectedIndex = sender.tag;
MyAnnotationObject * selectedObject = [self.annotations objectAtIndex:selectedIndex];
//现在您知道要显示哪个详细视图;
//之后的代码取决于你的应用程序的结构,但可能看起来像:
MyDetailViewController * detailView = [[MyDetailViewController alloc] initWithNibName ...];
detailView.detailObject = selectedObject;
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
Wanted some help with a problem with mapkit I am facing. Should be a silly problem or I have missed out something while going through the mapkit framework.
Here is the senario. I am placing multiple annotation on the map when the user performs some search like pizza. Added button for the right annotation view, on click which opens a next detail view. The problem is how to send some information to the next view, for example I add index to annotations while creating them, now I want to access this information from annotation, and pass it to the next view via the selector set on the button.
I have checked all the mapkit delicate, but don't find a one where I can map this information with the next view and annotation.
Hope I have not confused you guys in my question. Please let me know I will reframe it.
Thaking in advance.
When you create the UIButton
for the annotation, set the tag
property (tag is an NSInteger
property of UIView
) to an id or array index that identifies the relevant object. You can then retrieve that tag value from the sender
parameter to your selector.
Edit: here's some sample code.
You create your annotation view and associate the button in your delegate's -mapView:viewForAnnotation: method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Boilerplate pin annotation code
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"restMap"];
if (pin == nil) {
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"restMap"] autorelease];
} else {
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorRed
pin.canShowCallout = YES;
pin.animatesDrop = NO;
// now we'll add the right callout button
UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// customize this line to fit the structure of your code. basically
// you just need to find an integer value that matches your object in some way:
// its index in your array of MKAnnotation items, or an id of some sort, etc
//
// here I'll assume you have an annotation array that is a property of the current
// class and we just want to store the index of this annotation.
NSInteger annotationValue = [self.annotations indexOfObject:annotation];
// set the tag property of the button to the index
detailButton.tag = annotationValue;
// tell the button what to do when it gets touched
[detailButton addTarget:self action:@selector(showDetailView:) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = detailButton;
return pin;
}
Then in your action method, you'll unpack the value from tag
and use it to display the right detail:
-(IBAction)showDetailView:(UIView*)sender {
// get the tag value from the sender
NSInteger selectedIndex = sender.tag;
MyAnnotationObject *selectedObject = [self.annotations objectAtIndex:selectedIndex];
// now you know which detail view you want to show; the code that follows
// depends on the structure of your app, but probably looks like:
MyDetailViewController *detailView = [[MyDetailViewController alloc] initWithNibName...];
detailView.detailObject = selectedObject;
[[self navigationController] pushViewController:detailView animated:YES];
[detailView release];
}
这篇关于Mapkit具有多个注释(callout),映射下一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!