创建注释数组以便在地图上制作不同的标注视图
我正在使用 MapView 并放置了 30 个引脚位置,一切都运行良好。我在标注中添加了一个带有 rightCalloutAccessoryView 的按钮。这是按钮代码:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
[rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return pinView;
}
这工作正常并调用“rightButtonPressed”方法。下面是该方法的实现:
-(IBAction) rightButtonPressed:(id)sender
{
NSLog(@"button clicked");
MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
[thisMap switchViews:self.view toView:dvc.view];
}
因此,如您所见,每当我触摸所有 30 个引脚标注中的任何按钮时,它都会转到同一个视图 (DetailViewController)。我希望每个按钮都有自己的视图可以切换(这是我要放置“到这里的方向”和“从这里开始的方向”等以及商店地址和名称的位置)。
我意识到我可以制作 30 个视图并制作 30 种不同的方法来应用于每个引脚,但我知道必须有一个更简洁的代码来处理数组。
可能的某些数组可以在 DetailViewController 上的 UILabel 中调用,因此它只会加载适当的信息,并给出到适当位置的指示。
这可能是一个大问题,我四处寻找一些教程,但没有找到任何可以准确回答该问题的内容。如果有人能让我开始(或者甚至为我指明正确的方向),我将不胜感激。
I'm working with a MapView and have 30 pin locations placed and everything's working great. I added a button in the callout with a rightCalloutAccessoryView. Here's the button code:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
[rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return pinView;
}
This works fine and calls the "rightButtonPressed" method. Here's the implementation of that method:
-(IBAction) rightButtonPressed:(id)sender
{
NSLog(@"button clicked");
MapViewController *thisMap = (MapViewController *)[[UIApplication sharedApplication] delegate];
DetailViewController *dvc = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
[thisMap switchViews:self.view toView:dvc.view];
}
So, as you see, any time I touch any button from all 30 pin callouts, it goes to the same view (DetailViewController). I want each button to have it's own view to switch to (it's where I'm going to place "Directions To Here" and "Directions From Here" etc. as well as the store address and name).
I realize that I could make 30 views and make 30 different methods that could apply to each pin but I know there has to be a more concise code that deals with arrays.
Possible certain arrays could be called in a UILabel on the DetailViewController so it will just load the appropriate information and also give directions to the appropriate location.
This might be a big question and I've looked around for some tutorials but haven't found anything that answers the question exactly. If anyone could get me started (or even point me in the right direction), I'd be totally grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于注释视图标注附件,最好使用地图视图自己的委托方法
calloutAccessoryControlTapped
来处理按钮按下操作,而不是使用addTarget
和您自己的自定义方法。在 calloutAccessoryControlTapped 委托方法中,您可以使用 view.annotation 直接访问所点击的注释,而无需弄清楚所点击的数组或任何数据结构中的哪个注释。
删除对
addTarget
的调用,并将rightButtonPressed:
方法替换为:With annotation view callout accessories, it's much better to use the map view's own delegate method
calloutAccessoryControlTapped
to handle the button press instead of usingaddTarget
and your own custom method.In the
calloutAccessoryControlTapped
delegate method, you can directly access the annotation that was tapped usingview.annotation
without having to figure out which annotation in an array or whatever data structure was tapped.Remove the call to
addTarget
and replace therightButtonPressed:
method with: