我想要做的是基本上发送某种形式的id与我的按钮,所以我可以计算哪一个被点击。我的按钮被动态创建,并且它们都执行加载新视图的相同功能,我想知道哪个按钮被点击了,以便我可以在新视图上显示正确的数据。选择器添加参数
我有一个注释的NSMutableArray,我添加一个按钮的细节引脚。该按钮的作品,它加载下一个视图,但我想弄清楚哪个按钮被按下。
我使用一个名为数组的NSMutableArray的单例。单身的名字是助手
我所做的是:
Helper* array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
[myDetailButton setTag:clickedNum];
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
这就是我创建去见地图中的注释。下一行是我的功能ID
- (IBAction)moreInfo:(UIButton*)sender
而这正是我想要检索标签
Helper *array = [Helper sharedManager];
array.clicked = sender.tag;
当我运行这一点,点击按钮在我的注解之一,我得到一个异常说NSInvalidArgumentException,原因: - (的MapViewController moreInfo:事件:]
任何帮助将appriciated
编辑:
按照要求对助手的界面:
@interface Helper : NSObject {
NSMutableArray *array;
int clicked;
}
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic) int clicked;
+(id)sharedManager;
@end
此外,我认为明智的做法是添加:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = @"Events";
MKPinAnnotationView *retval = nil;
if ([annotation isKindOfClass:[Events class]])
{
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (retval == nil) {
retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
Helper *array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
// Set up the Left callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[myDetailButton setTag:clickedNum];
NSLog([NSString stringWithFormat:@"Testing tag: %@", clickedNum]);
[myDetailButton addTarget:self action:@selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];
// Set the image for the button
[myDetailButton setImage:[UIImage imageNamed:@"icon72.png"] forState:UIControlStateNormal];
// Set the button as the callout view
retval.leftCalloutAccessoryView = myDetailButton;
}
if (retval) {
[retval setPinColor:MKPinAnnotationColorGreen];
retval.animatesDrop = YES;
retval.canShowCallout = YES;
}
}
return retval;
}
这些按钮是否在viewForAnnotation方法中设置为标注附件视图? – Anna
@Anna是的,他们是 – Armand