2010-09-28 6 views
0

J'utilise UIActionSheet pour afficher 3 boutons d'action dans un popover lorsqu'une ligne de table est sélectionnée.NSRangeException liée à UIActionSheet

Dans .h de mon tableViewController, je l'ai déclaré ce qui suit:

UIActionSheet *actionSheet; 

Dans .m de mon tableViewController, j'ai le code suivant

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if (self.actionSheet == nil) { 
self.actionSheet = [[UIActionSheet alloc] 
         initWithTitle:@"Available Actions" 
      delegate:self 
      cancelButtonTitle:nil 
      destructiveButtonTitle:@"Delete" 
      otherButtonTitles:@"Button1", @"Button2", nil] ; 
} 
UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 
CGRect popoverRect = [[cell superview] convertRect:cell.frame toView:self.tableView]; 
[self.actionSheet showFromRect:popoverRect inView:self.tableView animated:YES]; 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSString *buttonTitle = [self.actionSheet buttonTitleAtIndex:buttonIndex]; 

    if ([buttonTitle compare:@"button1"] == NSOrderedSame) { 
     //...DO Something 

    } else if ([buttonTitle compare:@"button2"] == NSOrderedSame) { 
     //...Do sometihing 

     }else { 
     //... 
    } 
} 

La fiche d'action ne s'affiche correctement après la sélection d'une ligne de table, cependant, elle se bloque si je clique sur n'importe où en dehors de la feuille d'action, et la console affiche ce qui suit:En comparaison, il ne plante PAS et le popover de feuille d'action est rejeté correctement lorsque je clique sur n'importe quel bouton de la feuille d'action.

Répondre

4

N'utilisez pas buttonTitleAtIndex: et ne comparez pas les titres des boutons (ils peuvent être localisés). Comparez simplement buttonIndex à un nombre correspondant à l'index réel du bouton, en utilisant les propriétés destructiveButtonIndex et firstOtherButtonIndex.

0

Vérifiez si buttonIndex == -1 et si elle réaffiche votre feuille d'action plutôt que le code actuel de vos méthodes.