0

I am passing an array(which is coming from database) to action sheet. I have added cancel button but it is not cancelling.It is showing error array beyond bounds. Here is my code:

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Chatting Apps" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];

    for (int i =0;i<self.arrOfAppNames.count;i++) {

        [actionSheet addButtonWithTitle:[[self.arrOfAppNames objectAtIndex:i]objectAtIndex:1]];

    }
    [actionSheet addButtonWithTitle:@"Cancel"];
    actionSheet.cancelButtonIndex = [self.arrOfAppNames count];
    [actionSheet showInView:self.view];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Amit Limje
  • 41
  • 3
  • 2
    Just remove `objectAtIndex:1` from line `[actionSheet addButtonWithTitle:[[self.arrOfAppNames objectAtIndex:i]objectAtIndex:1]];` – VRAwesome Aug 14 '14 at 10:28
  • @SweetAngel you are right this is the solution for this question. you must put it as your answer. i will remove my answer if you wants to put – Mohit Aug 14 '14 at 11:23

2 Answers2

0

Use this code hope this helps you:

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Chatting Apps" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];

for (int i =0;i<self.arrOfAppNames.count;i++) {

    [actionSheet addButtonWithTitle:[[self.arrOfAppNames objectAtIndex:i]objectAtIndex:1]];

}
[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = (int)[self.arrOfAppNames count];
[actionSheet showInView:self.view];
Mohit
  • 3,708
  • 2
  • 27
  • 30
BHASKAR
  • 1,201
  • 1
  • 9
  • 20
  • 1
    it is still not working. Cancel button is getting displayed there but when I click on it app is getting crashed with error array beyond bounds – Amit Limje Aug 14 '14 at 12:12
  • please nslog [self.arrOfAppNames count] before set cancelButtonIndex and uiactionsheet delegate method nslog buttonindex – BHASKAR Aug 14 '14 at 12:15
  • @AmitLimje did you try what sweet angel said? – Mohit Aug 14 '14 at 13:15
  • @MohitPopat What sweet angel said was right but I am getting array from database. Hence I have done like that – Amit Limje Aug 20 '14 at 06:32
0

This is what works for me when I add a Cancel button to an action sheet:

[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
dopl
  • 66
  • 8
  • 1
    it is still not working. Cancel button is getting displayed there but when I click on it app is getting crashed with error array beyond bounds – Amit Limje Aug 14 '14 at 12:14
  • If your app is crashing *after* the cancel button is tapped, then it sounds like the problem might be in the way the app handles the cancel tap. You might want to look into your code for `UIActionSheetDelegate` methods. – dopl Aug 15 '14 at 21:36