Rashad
Rashad

Reputation: 11197

UIActionSheetdelegate not getting subviews

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?

Upvotes: 1

Views: 749

Answers (2)

Henit Nathwani
Henit Nathwani

Reputation: 442

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..

Upvotes: -2

Kanan Vora
Kanan Vora

Reputation: 222

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...

Upvotes: 1

Related Questions