如何在UIActionSheet中禁用按钮?

时间:2009-07-22 05:57:49

标签: iphone

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

由于

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

Reuven的版本略有改进[唉,我无法对他的评论添加评论,因为我没有声誉'尚未...]。

@interface UIActionSheet (ButtonEnabled)
- (void)setButtonAtIndex:(NSUInteger)index Enabled:(BOOL)enabled;
@end

@implementation UIActionSheet (ButtonEnabled)
- (void)setButtonAtIndex:(NSUInteger)index Enabled:(BOOL)enabled
{
    for (UIView* view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            if (index-- == 0) {
                [(UIButton*)view setEnabled:enabled];
                break;
            }
        }
    }
}
@end

之前的respondsToSelector:检查是无关紧要的,因为我们已经在检查UIButton(这是一个UIControl子类,它都支持setEnabled)。为了与Apple的NSArrays保持一致,我也改为" AtIndex& #34;和NSUInteger,但这主要是化妆品。

总体方法似乎工作正常,但请注意它确实假设按钮子视图的顺序与构建操作表时按钮索引的顺序精确匹配,在AFAIK的任何地方都没有严格记录。< / p>

答案 2 :(得分:2)

Swift 3.0版本:

let actionSheet = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.actionSheet)


actionSheet.addAction(UIAlertAction(title:"Modify", style:UIAlertActionStyle.default, handler:{ action in
    // Do your thing here
}))

let disabledDelete = UIAlertAction(title:"Cannot delete this item", style:UIAlertActionStyle.destructive, handler:nil)
disabledDelete.isEnabled = false
actionSheet.addAction(disabledDelete)

actionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.cancel, handler:nil))

self.present(actionSheet, animated:true, completion:nil)

答案 3 :(得分:1)

提供的解决方案无法在iOS 8上运行。如果有人没有使用UIAlertController(这是Apple推荐的方式),这就是我修改@ Reuven&#39的方式;答案也适用于iOS 8:

(第二阶段基于this所以回答)

- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled {

    SEL selector = NSSelectorFromString(@"_alertController");

    //iOS 8
    if ([self respondsToSelector:selector]) {

        UIAlertController *alertController = [self valueForKey:@"_alertController"];

        if ([alertController isKindOfClass:[UIAlertController class]]){

            UIAlertAction *action = alertController.actions[buttonIndex];

            [action setEnabled:enabled];

        }

    //iOS 7
    }else{

        for (UIView* view in self.subviews){

            if ([view isMemberOfClass:NSClassFromString(@"UIAlertButton")]) {

                if (buttonIndex == 0) {

                    if ([view respondsToSelector:@selector(setEnabled:)]){

                        UIButton* button = (UIButton*)view;

                        button.enabled = enabled;

                    }

                }

                buttonIndex--;

            }

        }

    }

}

答案 4 :(得分:0)

这可能有点晚了。但是,我想出的方法是创建UIButton并添加它们 到UIActionSheet子视图。确保这些按钮位于顶部并完全覆盖要更换的默认UIActionSheet按钮。当UIButton放在默认的UIActionSheet按钮上时,其UIResponder优先于UIActionSheet按钮UIResponder。因此,这样做可以禁用和启用这些按钮,但是你喜欢UIViewController逻辑中的任何地方。这可能是访问SDK的私有方法的替代方法(如上所述 - UIThreePartButton),苹果可能会拒绝您的应用。我相信这符合Apple的指导方针。

// Instantiate once
if (self.actionSheet==nil)  {
    UIActionSheet *as = [[UIActionSheet alloc] 
                         initWithTitle:@""
                         delegate:self 
                         cancelButtonTitle:@"Cancel"
                         destructiveButtonTitle:@"Load Data"
                         otherButtonTitles:@"Update Data",nil];

    //[actionSheet showInView:self.view];
    self.loadUIButton.frame = CGRectMake(24.0f,25.0f,275.f,46.0f);
    [as addSubview: self.loadUIButton];
    self.updateUIButton.frame = CGRectMake(24.0f,78.0f,275.f,46.0f);
    [as addSubview: self.updateUIButton];
    //[actionSheet addSubview: self.cancelUIButton];
    //[as showFromToolbar: self.navigationController.toolbar];
    self.actionSheet = as;
    [as release];
}
[self.actionSheet showFromToolbar: self.navigationController.toolbar];

答案 5 :(得分:-2)

Thees按钮是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

        }
    }