0

I've been searching the internet and trying different thing but cannot seem to change the button title colour on my action sheet.

Here is the action sheet code.

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

I've tried this delegate but it does not work.

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.textColor = [UIColor greenColor];
        }
    }
}
user2920762
  • 223
  • 1
  • 8
  • 17
  • 1
    check out this http://stackoverflow.com/a/4248450/1865424 – Kundan May 02 '14 at 10:49
  • 1
    possible duplicate of [UIActionSheet button's color](http://stackoverflow.com/questions/4248400/uiactionsheet-buttons-color) – Michael May 02 '14 at 10:51
  • i think Your code seems to be a perfect. put brekpoint willPresentActionSheet is called or not – Sunny Shah May 02 '14 at 10:56
  • did you add UIActionSheet delegate in .h file? – Ramdhas May 02 '14 at 10:57
  • Make sure you set, UIActionSheet delegate and set breakpoint on this method, - (void)willPresentActionSheet:(UIActionSheet *)actionSheet, check whether this method getting called . – Ramdhas May 02 '14 at 10:59
  • Yes UIActionSheetDelegate is in my .h file. I've tried breakpoints on every line within willPresentActionSheet and they all break out. So the method is been invoked. Just no colour changes – user2920762 May 02 '14 at 11:13

2 Answers2

0

Make sure you set, UIActionSheet delegate and set breakpoint on this method, willPresentActionSheet, check whether this method getting called .

If you have set delegate means, try this one

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

        }
    }
}

Update:

  1. https://github.com/seivan/SHActionSheetBlocks
  2. https://github.com/ianb821/IBActionSheet
Ramdhas
  • 1,765
  • 1
  • 18
  • 26
  • Is changeTextColorForUIActionSheet a delegate for actionSheet? If I copy this directly it does not execute. Yes UIActionSheetDelegate is in my .h file – user2920762 May 02 '14 at 11:13
  • Make sure to run that code, AFTER you call : [actionSheet showInView]; – Ramdhas May 02 '14 at 11:22
  • called [self changeTextColorForUIActionSheet:actionSheet]; after [actionSheet showInView:[self.view window]];, still no luck – user2920762 May 02 '14 at 12:01
  • https://github.com/seivan/SHActionSheetBlocks , https://github.com/ianb821/IBActionSheet just try this sample – Ramdhas May 02 '14 at 12:27
0

You must implement UIActionSheetDelegate and then you can use willPresentActionSheet delegate method

- (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];
        }
    }
}
Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59
Erhan
  • 908
  • 8
  • 19