2014-05-02 119 views
0

我一直在寻找互联网,尝试不同的事情,但似乎无法改变我的行动表上的按钮标题颜色。如何设置UIActionSheet按钮颜色

以下是操作表代码。

- (void)showActionSheet 
{ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
            initWithTitle:@"Select Country" 
            delegate:self 
            cancelButtonTitle:nil 
            destructiveButtonTitle:nil 
            otherButtonTitles:nil]; 

    for (NSString *listCountry in resultSet) [actionSheet addButtonWithTitle:listCountry]; 

    [actionSheet showInView:[self.view window]]; 
} 

我试过这个委托,但它不起作用。

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    for (UIView *subview in actionSheet.subviews) { 
     if ([subview isKindOfClass:[UIButton class]]) { 
      UIButton *button = (UIButton *)subview; 
      button.titleLabel.textColor = [UIColor greenColor]; 
     } 
    } 
} 
+1

检查出这个http://stackoverflow.com/a/4248450/1865424 – Shivaay

+1

[UIActionSheet按钮的颜色]的可能重复(http://stackoverflow.com/questions/4248400/uiactionsheet-buttons-color ) – Michael

+0

我认为你的代码似乎是一个完美的。放置brekpoint willPresentActionSheet是否被调用 –

回答

0

确保你设定,这种方法UIActionSheet委托并设置断点, willPresentActionSheet, 检查这个方法是否获取调用。

如果已经设置委托手段,试试这个

- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet { 
    UIColor *tintColor = [UIColor redColor]; 

    NSArray *actionSheetButtons = actionSheet.subviews; 
    for (int i = 0; [actionSheetButtons count] > i; i++) { 
     UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i]; 
     if([view isKindOfClass:[UIButton class]]){ 
      UIButton *btn = (UIButton*)view; 
      [btn setTitleColor:tintColor forState:UIControlStateNormal]; 

     } 
    } 
} 

更新:

  1. https://github.com/seivan/SHActionSheetBlocks
  2. https://github.com/ianb821/IBActionSheet
+0

changeTextColorForUIActionSheet是否为actionSheet的委托?如果我直接复制它,它不会执行。是的UIActionSheetDelegate在我的.h文件中 – user2920762

+0

确保运行该代码后,您调用:[actionSheet showInView]; – Ramdy

+0

调用[self changeTextColorForUIActionSheet:actionSheet]; [actionSheet showInView:[self.view window]];后,仍然没有运气 – user2920762

0

您必须实现UIActionSheetDelegate,然后你可以使用willPresentActionSheet委托方法d

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    for (UIView *subview in actionSheet.subviews) { 
     if ([subview isKindOfClass:[UIButton class]]) { 
      UIButton *button = (UIButton *)subview; 
      [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     } 
    } 
}