iOS: Tagging Errors

Go To StackoverFlow.com

0

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 {
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"Yes"
                                                    otherButtonTitles:@"No", nil];
    [deleteAlertView show];
    int imageIndex = sender.tag;
    deleteAlertView.tag = imageIndex;

}

- (void)deleteAlertView:(UIAlertView *)deleteButtonPressed
       didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [deleteButtonPressed cancelButtonIndex]) {

        [array removeObjectAtIndex:deleteButtonPressed.tag];

    }
  [self.user setObject:array forKey:@"images"];
}

The error that I'm getting is highlighting int imageIndex = sender.tag;, and states "Property tag not found for object of type __strong id". I've been doing research regarding this error, and am not finding any helpful information. I'm still new to programming, so I'm not sure how to fix this at all. Any help is much appreciated, thank you!

2012-04-03 21:48
by Henry F


4

You need to cast sender to appropriate type. For instance int imageIndex = ((UIView*)sender).tag;.

2012-04-03 21:58
by Alejandro
Cool, thanks a lot. I added NSLog(@"Sender is %@", sender); in my deleteButtonPressed method, and it said that the sender is UIButton. So, I added int imageIndex = ((UIButton *)sender).tag;. The error went away, but when I click Yes in the alert view, nothing happens. Should I create another question for this - Henry F 2012-04-03 22:35
Joey, you’ve specified your button names in the wrong order, and (going by the iOS interface guidelines) they’re also kind of poorly labeled. Your alert-view call should be more like …delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Delete", nil. The actual logic error is that your “Yes” button is labeled as the cancel button, and your delegate method is checking that the button pressed was not the cancel button. You can leave the delegate method as-is, though: just swap the order of your button texts when you’re setting up the alert view, and, ideally, name them more clearly - Noah Witherspoon 2012-04-03 23:05
@Noah Witherspoon Thanks a lot, I just changed that now. The issue is still present though, should I make another question regarding why the image won't delete - Henry F 2012-04-03 23:16
Probably, yeah - Noah Witherspoon 2012-04-03 23:34
Ads