简体   繁体   中英

Action sheet cancel button not cancelling action sheet

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];

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];

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

[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM