在显示之前更改UIActionsheet按钮文本和背景

时间:2014-03-28 06:45:40

标签: ios uiactionsheet

我显示动作表以选择号码。但现在我想在动作表中显示默认的选定按钮。

代码:

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

在显示

之前更改按钮文字和背景
- (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]];

            }
        }
    }
}

编辑:

它们没有错误,但显示的是动作表,并且没有选择按钮。

2 个答案:

答案 0 :(得分:1)

这是解决方案

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

答案 1 :(得分:0)

UIActionSheet没有所选按钮的概念。模态视图控制器可能更合适。或者,如果您使用的是iPad,则可以使用内置UIPopoverController的{​​{1}}。