Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Disabling UIActionSheet buttons

I want to disable buttons in the UIAction sheet and enable them after a certain condition is true. How do I achieve this? Any ideas?

like image 325
shrads Avatar asked Apr 13 '09 11:04

shrads


2 Answers

I found that craig's answer didn't work for me (on OS 3.1). After a little digging around I discovered that the subviews of UIActionSheet are actually of an undocumented class UIThreePartButton

Anyway, this works for me (implemented as part of the UIActionSheetDelegete method)

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet  // before animation and showing view
{
    for (UIView* view in [actionSheet subviews])
    {
        if ([[[view class] description] isEqualToString:@"UIThreePartButton"])
        {
            if ([view respondsToSelector:@selector(title)])
            {
                NSString* title = [view performSelector:@selector(title)];
                if ([title isEqualToString:@"Button 1"] && [view respondsToSelector:@selector(setEnabled:)])
                {
                    [view performSelector:@selector(setEnabled:) withObject:NO];
                }
            }
        }
    }
}

Hope that helps someone else, although I'd echo Ed Marty's question of whether you'd be better off just omitting these buttons from the action sheet altogether instead of doing this. As always when using undocumented features, there is a risk of app store rejection, although this code is written to fail gracefully if Apple do chnage the APIs again in a future OS release.

like image 161
cidered Avatar answered Nov 17 '22 08:11

cidered


Is there a circumstance that can change, while the action sheet is open, that could cause the button to become enabled? If not, I think the better approach is to alter the buttons that the sheet displays based on your condition.

Otherwise, the only way of handling this is to iterate through the sheet's subviews, like Craig said, and look for the UIButton objects. I'd be careful about using the button's title, though, because the title could (and should!) be localized for different languages. So comparisons against the title aren't all that reliable. Since you didn't create the button, you don't really know what the tag or action of each button would be, either, so that's a bit difficult, too.

Presumably, the buttons will appear in the subviews array in the order you specified them to the UIActionSheet, but since this isn't documented, there's no guarantee that they will appear in that order, or that they will continue to appear in that order in future releases of the Cocoa Touch SDK. Because of that, I'd worry mainly about being rejected from the App Store for using undocumented functionality.

like image 2
Alex Avatar answered Nov 17 '22 09:11

Alex