我想做的基本上是用我的按钮发送某种形式的 ID,这样我就可以计算出哪个按钮被点击了。我的按钮是动态创建的,它们都执行加载新 View 的相同功能,我想知道单击了哪个按钮,以便我可以在新 View 上显示正确的数据。
我有一个 NSMutableArray 的注释,我在 pin 的详细信息中添加了一个按钮。该按钮有效并加载了下一个 View ,但我想弄清楚按下了哪个按钮。
我使用一个带有 NSMutableArray 的单例,称为数组。单例名称为Helper
我所做的是:
Helper* array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
[myDetailButton setTag:clickedNum];
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
这就是我创建 map 注释的地方。下一行是我的函数ID
- (IBAction)moreInfo:(UIButton*)sender
这是我要检索标签的地方
Helper *array = [Helper sharedManager];
array.clicked = sender.tag;
当我运行它并单击我的一个注释中的按钮时,我得到一个异常,提示 NSInvalidArgumentException,原因:-(MapViewController moreInfo:event:]
我们将不胜感激
编辑:
根据 helper 接口(interface)的要求:
@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;
}
最佳答案
您的方法 moreInfo:
与您在
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
所以把它改成
[myDetailButton addTarget:self action:@selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];
或者改变方法定义。
关于iphone - 选择器添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6693163/