Question

I show actionsheet to select number. but now I want to show the default selected button in actionsheet.

Code :

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Number"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil otherButtonTitles:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];


[actionSheet showInView:self.view];

Change Button Text and Background before Showing

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {

        if ([subview isKindOfClass:[UIButton class]]) {

            UIButton *button = (UIButton *)subview;

            [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

            if ([button.titleLabel.text isEqualToString:@"5"]) {

                button.selected = true;
                [button setBackgroundColor:[UIColor blueColor]];

            }
        }
    }
}

Edit:

Their is no error but actionsheet is shown and none of button is selected.

Was it helpful?

Solution

here is the solution

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{

   for (UIView* subview in actionSheet.subviews) {
      for (UIView *tView in subview.subviews) {
          if ([tView isKindOfClass:[UITableView class]]) {
            UITableView *actionTableView = (UITableView *)tView;
            NSIndexPath *myIP = [NSIndexPath indexPathForRow:5 inSection:0];
            UITableViewCell *cell = [actionTableView cellForRowAtIndexPath:myIP];
            cell.backgroundColor = [UIColor blackColor];
              for (UIView *cells in cell.subviews) {
                for (UIView *subCells in cells.subviews) {
                    if ([subCells isKindOfClass:[UILabel class]]) {
                        UILabel *cellText = (UILabel *)subCells;
                        cellText.textColor = [UIColor redColor];
                    }
                }
            }
        }
    }


    if ([subview isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)subview;
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        if ([button.titleLabel.text isEqualToString:@"5"]) {
            button.selected = true;
            [button setBackgroundColor:[UIColor blueColor]];
        }
    }
 }
}

OTHER TIPS

UIActionSheet doesn't have the concept of selected button. Probably a modal view controller is more suitable. Or if you are on iPad you can use a UIPopoverController with a UITableView inside.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top