UIActionSheetdelegate not getting subviews

751 Views Asked by At

In my iOS 8 application I am using UIActionSheet. I tried to change the color of the button title in willPresentActionSheet delegate method, but it is not recognizing buttons as its subview.

I constructed UIActionSheet like this:

UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select an Option" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
popup.tag = 2;
[popup addButtonWithTitle:@"Add Tag To Chat"];
[popup addButtonWithTitle:@"Remove Tag From Chat"];
[popup addButtonWithTitle:@"Terminate"];
[popup addButtonWithTitle:@"Cancel"];
[popup showInView:self.view];

Delegate is:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    NSLog(@"%d",[actionSheet.subviews count]);
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
    }
}

Buttons are showing, I can click and action performs. But it says count = 0. Why?

2

There are 2 best solutions below

2
Henit Nathwani On

The delegate methods in which you're trying to check the number of subviews: willPresentActionSheet seems inappropriate.

willPresentActionSheet is called before the action sheet actually gets rendered.

If you check the number of subviews in didlPresentActionSheet, that will be more appropriate.

Hope this helps..

0
Kanan Vora On

Try this way, changing the UIColor of subviews:

    UIActionSheet * action = [[UIActionSheet alloc]
                              initWithTitle:@"Title"
                              delegate:self
                              cancelButtonTitle:@"Cancel"
                              destructiveButtonTitle:nil
                              otherButtonTitles:@"",nil];
[[[action valueForKey:@"_buttons"] objectAtIndex:0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

I've tried it. It is working...