2011-07-14 44 views
3

我想要做的是基本上发送某种形式的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; 
} 
+0

这些按钮是否在viewForAnnotation方法中设置为标注附件视图? – Anna

+0

@Anna是的,他们是 – Armand

回答

4

你的方法moreInfo:不匹配,你已经在

设置为选择
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside]; 

因此改为

[myDetailButton addTarget:self action:@selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside]; 

或更改方法定义。

+0

+1这正是我要写的,所以它*必须*是正确的。 – Caleb

+0

@Deepak我改变了选择器。它现在进入下一个视图,但array.clicked的内容现在(空),当我把它放在UILable中 – Armand

+0

@Armand你可以添加'Helper'接口吗?你如何在标签中设置它?记录价值是否给你正确的价值? –

0

虽然Deepak的答案是正确的,并且可以正常工作,但由于您的按钮是注释标注附件视图,因此可以通过一种更简单的方式来确定选择了哪个注释按钮。

而不是添加您自己的目标+动作,使用地图视图的mapView:annotationView:calloutAccessoryControlTapped:委托方法,它将在点击标注视图时被调用。在那里,注释可用作view.annotation。您不需要设置按钮标签或使用注释的索引。

有关示例,请参阅this other answer

另一个问题是,在你的viewForAnnotation方法中,你打电话给dequeueReusableAnnotationViewWithIdentifier,但没有对结果做任何事情(所以没有得到任何重用)。相反,它应该分配到retval和最后if,设置retval.annotation

+0

我不知道如何使用那种微妙的方法。我不完全明白你在说什么dequeueReusable ..... – Armand

+0

您已经编写的viewForAnnotation方法也是地图视图在需要注解视图时调用的委托方法。 calloutAccessoryControlTapped方法只是另一个像viewForAnnotation的委托方法,您可以实现,并且地图视图会在点按标注按钮时调用它。对于出队,可能[文档](http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html%23//apple_ref/occ/cl/MKMapView)将有所帮助。 – Anna

+0

此外,在[位置感知编程指南的注释地图页面]上搜索“出列”(http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html )。 – Anna