如何在UIActionSheet中禁用按钮?

sat*_*ish 8 iphone

我需要禁用UIActionSheet中的按钮.经过一些操作后,我需要再次启用它们.有没有办法做到这一点.

谢谢

Reu*_*ven 17

基于一些线程,我在UIActionSheet上汇总了一个类别的答案,添加了一个setButton:toState方法,如下所示.希望能帮助到你:

@interface UIActionSheet(ButtonState)
- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enbaled;
@end

@implementation UIActionSheet(ButtonState)
- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled {
    for (UIView* view in self.subviews)
    {
        if ([view isKindOfClass:[UIButton class]])
        {
            if (buttonIndex == 0) {
                if ([view respondsToSelector:@selector(setEnabled:)])
                {
                    UIButton* button = (UIButton*)view;
                    button.enabled = enabled;
                }
            }
            buttonIndex--;
        }
    }
}
@end
Run Code Online (Sandbox Code Playgroud)


oxi*_*gen -2

这些按钮是 UIActionSheet 的子视图,它们的类是 UIThreePartButton

你可以获得它们并做你想做的一切:

UIActionSheet *a = [[UIActionSheet alloc]initWithTitle:@"" delegate: nil cancelButtonTitle: @"c" destructiveButtonTitle: @"d" otherButtonTitles: @"ot", nil];
    [a showInView: window];

    for(UIView *v in [a subviews])
    {
        if([[v description] hasPrefix: @"<UIThreePartButton"] )
        {
            v.hidden = YES;  //hide
           //((UIButton*)v).enabled = NO;   // disable

        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 这不仅仅是被 Apple 拒绝,使用私有 API 还表现出对客户的不尊重。私有 API 之所以是私有的,是因为 Apple 仍在锁定它们,因此如果它们发生变化,您的应用程序就会崩溃。如上所述的视图遍历导致许多应用程序在 3.0 版本下崩溃,以至于 Apple 在发行说明中指出了这一点并表示不要这样做。 (6认同)
  • 我建议您不要使用此方法,因为这些方法是 SDK 私有的。Apple 可能会拒绝您的应用程序。最好的选择是根本不显示这些按钮,而不是禁用它们 (3认同)