I have an app that displays 9 UIImageViews, and I'm trying to add in the ability for the user to delete them. So, I set it all up, and set up an alert view for the user to confirm that they want to delete the image that they tapped on. When I click Yes in the alert view, the app crashes and the debugger states:
2012-04-04 18:19:44.311 AppName[1857:f803] -[UIControl setImage:]: unrecognized selector sent to instance 0x6aac690
2012-04-04 18:19:44.312 AppName[1857:f803] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControl setImage:]: unrecognized selector sent to instance 0x6aac690'
The strange part is that I'm not calling setImage in any of my code. This is the code I'm working with:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;
self.array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
if (imageView2.image == nil) {
imageView2.image = img;
NSLog(@"The image is a %@", imageView);
[self.array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];
return;
}
}
.....
- (IBAction)deleteButtonPressed:(id)sender {
NSLog(@"Sender is %@", sender);
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
message:@"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[deleteAlertView show];
int imageIndex = ((UIButton *)sender).tag;
deleteAlertView.tag = imageIndex;
}
- (void)alertView: (UIAlertView *) alertView
clickedButtonAtIndex: (NSInteger) buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"User Clicked Yes. Deleting index %d of %d", alertView.tag, [array count]);
NSLog(@"The tag is %i", alertView.tag);
[self.array removeObjectAtIndex: alertView.tag];
NSLog(@"After deleting item, array count = %d", [array count]);
NSLog(@"Returned view is :%@, in view: %@", [self.view viewWithTag:alertView.tag], self.view);
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}
[self.user setObject:self.array forKey:@"images"];
}
I added breakpoints, and it appears that the line that is causing the crash is:
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
The purpose of that line is to delete the image from the UI. I'm not sure why that line is causing this error though, the code appears to be correct. The nib file in question is a UIControl, rather than just a UIView, so that may be the issue. Any help or advice is much appreciated, thanks!
Turns out that I had my tags messed up. I tagged the invisible button over the UIImageView, forgetting that it was not the actual UIImageView. That explains all of the weirdness. I tagged the actual UIImageViews this time in accordance with the UIButtons, and it works fine now. Thanks everyone for all of your help.
A UIControl does not have an image property. You are mistaking the imageView for a UIControl