I am using UIActionSheet
to display 3 action buttons in a popover when a table row is selected.
In .h of my tableViewController
, I have declared the following:
UIActionSheet *actionSheet;
In .m of my tableViewController
, I have the following code
- (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 {
//...
}
}
The Action Sheet does show up correctly after selecting a table row, however, it crashes if I click on anywhere outside the action sheet, and the console displays the following:
Terminating app due to uncaught exception 'NSRangeException', reason:
'*** -[NSCFArray objectAtIndex:]: index (-1( or possibly larger)) beyond bounds (3)'
In comparision, it does NOT crash and the action sheet popover gets dismissed correctly when I click on any buttons on the action sheet.