What I want to do is basically send some form of id with my button so I can calculate which one has been clicked. My buttons get dynamically created and all of them execute the same function that loads a new view, I want to know which button was clicked so I can display the correct data on the new view.
I have an NSMutableArray of annotations that I add a button to the details of the pin. The button works and it loads the next view, but I want to figure out which button was pressed.
I use a singleton with an NSMutableArray called array. The singleton name is Helper
What I did was:
Helper* array = [Helper sharedManager];
int clickedNum = [array.array indexOfObject:annotation];
[myDetailButton setTag:clickedNum];
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
That is where i create the annotations that go unto the map. The next line is my function ID
- (IBAction)moreInfo:(UIButton*)sender
And this is where I want to retrieve the tag
Helper *array = [Helper sharedManager];
array.clicked = sender.tag;
When I run this and click on the button in one of my annotations I get an exception saying NSInvalidArgumentException, reason: -(MapViewController moreInfo:event:]
Any help would be appriciated
Edit:
As requested the interface for helper:
@interface Helper : NSObject {
NSMutableArray *array;
int clicked;
}
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic) int clicked;
+(id)sharedManager;
@end
Also I thought it wise to add:
- (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;
}
Your method moreInfo: doesn't match what you have set as the selector in
[myDetailButton addTarget:self action:@selector(moreInfo:event:) forControlEvents:UIControlEventTouchUpInside];
So change that to
[myDetailButton addTarget:self action:@selector(moreInfo:) forControlEvents:UIControlEventTouchUpInside];
Or change the method definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With