1

We have a UIActionSheet with many options. In iOS 7 on iPad when scrolling the list, it ghosts the original values. Is this an iPad 7 bug or is there a change we can make? iOS 6 iPad, and iPhone looks ok to us.

before scrolling

after scrolling

//
//  ViewController.m
//  AlertViewTest
//
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _data = [NSMutableArray new];
    [_data addObject:@"Arthur"];
    [_data addObject:@"Bertha"];
    [_data addObject:@"Cristobal"];
    [_data addObject:@"Dolly"];
    [_data addObject:@"Edouard"];
    [_data addObject:@"Fay"];
    [_data addObject:@"Gonzalo"];
    [_data addObject:@"Hanna"];
    [_data addObject:@"Isaias"];
    [_data addObject:@"Josephine"];
    [_data addObject:@"Kyle"];
    [_data addObject:@"Laura"];
    [_data addObject:@"Marco"];
    [_data addObject:@"Nana"];
    [_data addObject:@"Omar"];
    [_data addObject:@"Paulette"];
    [_data addObject:@"Rene"];
    [_data addObject:@"Sally"];
    [_data addObject:@"Teddy"];
    [_data addObject:@"Vicky"];
    [_data addObject:@"Wilfred"];

    [_data addObject:@"Ana"];
    [_data addObject:@"Bill"];
    [_data addObject:@"Claudette"];
    [_data addObject:@"Danny"];
    [_data addObject:@"Erika"];
    [_data addObject:@"Fred"];
    [_data addObject:@"Grace"];
    [_data addObject:@"Henri"];
    [_data addObject:@"Ida"];
    [_data addObject:@"Joaquin"];
    [_data addObject:@"Kate"];
    [_data addObject:@"Larry"];
    [_data addObject:@"Mindy"];
    [_data addObject:@"Nicholas"];
    [_data addObject:@"Odette"];
    [_data addObject:@"Peter"];
    [_data addObject:@"Rose"];
    [_data addObject:@"Sam"];
    [_data addObject:@"Teresa"];
    [_data addObject:@"Victor"];
    [_data addObject:@"Wanda"];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)showAlert:(id)sender {
    if([self.actionSheet isVisible]) {
        [self.actionSheet dismissWithClickedButtonIndex:[self.actionSheet cancelButtonIndex] animated:YES];

        self.actionSheet.delegate = nil;
        self.actionSheet = nil;

        return;
    }

    if(nil == self.actionSheet) {
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select one"
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:nil];


        if(![self shouldPresentActionSheet:self.actionSheet]) {
            self.actionSheet = nil;
            return;
        }


        [self.actionSheet setCancelButtonIndex:[self.actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel")]];

    }

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [self.actionSheet showFromBarButtonItem:self.button animated:YES];
    } else {
        [self.actionSheet showFromToolbar:self.toolbar];
    }
}

-(BOOL)shouldPresentActionSheet:(UIActionSheet *)actionSheet
{
    if(actionSheet == self.actionSheet) {
        if([self.data count] == 0) {
            return NO;
        }
        for(NSString *d in self.data) {
            [self.actionSheet addButtonWithTitle:d];
        }

    }
    return YES;
}

@end
Jason Hocker
  • 6,585
  • 9
  • 39
  • 76
  • Consider using another interface (a table view in a popover). Action sheet was never meant to be hammered like this, and really you are just using it as a lazy man's table view. – matt May 21 '14 at 20:18
  • 1
    I would say you're misusing UIActionSheet. It's meant for not more than 10 action items the user can choose from. Of course, that's not a hard number, but showing an entire list in an action sheet is not a good idea. I would create a separate view controller with a table view (which is essentially what an action sheet is) that lets the user choose. – Scott Berrevoets May 21 '14 at 21:20
  • After I posted, I almost edited it to make a comment to protect my credibility. Often this will be 2-4 items, but we have one client using it in an odd way. What I posted was just sample code to show off what I think is a bug. – Jason Hocker May 22 '14 at 13:36
  • Check out [link](http://stackoverflow.com/questions/19025852/xcode-ipad-uiactionsheet-with-many-buttons-do-not-correctly-displayed-ios7) for a workaround – Flores Robles Jun 17 '14 at 18:01
  • I may be wrong, but it seems this is fixed in iOS 8 – Maxwell Apr 15 '15 at 16:19

2 Answers2

0

@Flores in the comments was right, here is how you can solve this issue:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    actionSheet.backgroundColor = [UIColor whiteColor];
    for (UIView *subview in actionSheet.subviews) {
        subview.backgroundColor = [UIColor whiteColor];
    }
}
Community
  • 1
  • 1
meda
  • 43,711
  • 13
  • 85
  • 120
-1

It should be something like this

//view did load stuff

- (IBAction)showAlert:(id)sender {
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

// ObjC Fast Enumeration
for (NSString *title in _data) {
    [actionSheet addButtonWithTitle:title];
}

[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = [_data count];

[actionSheet showInView:self.view];

Source

Community
  • 1
  • 1