Reputation: 4980
I'm working on an app that behaves like a photo gallery, and I'm implementing the option to have the user delete photos from their gallery. To accomplish this, I decided to place an invisible button over each picture. When the user hits an "Edit" button, the hidden delete buttons over each picture become active. I'm using the same IBOutlet over each of the hidden buttons for simplicity, and I've tagged each button appropriately in Interface Builder. When the user taps the button over the picture, an alert view appears asking if they really want to delete it. If they click yes, I call removeObjectAtIndex. Here is the code I'm using:
- (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]);
[self.array removeObjectAtIndex: alertView.tag];
NSLog(@"After deleting item, array count = %d", [array count]);
alertView.tag.image = nil;
}
[self.user setObject:self.array forKey:@"images"];
}
The issue here is alertView.tag.image
. I have an error stating "Member reference base type NSInteger (aka int) is not a structure or union. This code deletes the image out of the array just fine, but I still need to delete the image from the UI as well. I thought that alertView.tag.image
would have done the trick. I have no idea how to do this, I'm still new to Objective-C and the book I read does not cover any of this at all. I was also wondering how I could refresh the UI after deleting the image?
Upvotes: 0
Views: 1439
Reputation: 10251
tag is just NSInteger. It doesn have property called image. You should use,
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
Upvotes: 1
Reputation: 3300
How are you adding the image to the UI? Set the image in the UIImageView to another image or remove it from the view.
You cannot add .image
after .tag
. Tag is an integer instance field used to denote one view from another (among other things). Tag itself, does not have any properties, other than its value.
Upvotes: 0
Reputation: 31294
You're trying to access a property that doesn't exist:
alertView.tag.image = nil;
Your alertView
is a UIAlertView
- and the tag
is an integer property, which represents the int value you've assigned to the view (assuming you've done so). tag
is just a plain old int - it's a primitive.
What you instead need to do is take the tag value and call viewWithTag
on the superview that's holding your images: this will give you a reference to the image view, which you can then removeFromSuperview
as required.
Upvotes: 1