iOS禁用以编程方式创建的按钮,直到功能完成

时间:2015-01-07 11:17:46

标签: ios objective-c uibutton

您好,并提前感谢您的耐心和帮助:)。

这是我正在做的事情: - 我以编程方式创建多个调用相同功能的按钮

我想要完成的事情: - 按下按钮后,我希望运行公共功能并禁用所有按钮,以便在执行第一次呼叫期间不再调用该功能。在第一次通话结束后,我想重新启用按钮。

以下是我创建按钮的方法:

@interface ViewController ()
{
    UIButton *button;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int tagForTheButton = 0;

    int yPossition = 0;
    int xPossition = 0;



    for (int numberOfTheColomn = 0; numberOfTheColomn < 6; numberOfTheColomn++) {


                button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.adjustsImageWhenHighlighted = NO;
                button.frame = CGRectMake(xPossition, yPossition, 64, 64);
                button.tag = tagForTheButton;
                [button setTitle:title forState:UIControlStateNormal];
                [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                [self.scroll addSubview:button];
                yPossition=yPossition+100;
                tagForTheButton++;
            }




}

-(void)buttonPressed:(id)sender{

    //This is my attempt to disable the buttons
    //previous mistake used 2578
    for (int numberForIncrease; numberForIncrease<6; numberForIncrease++) {
        if (button.tag == numberForIncrease) {
            UIButton *btn = (UIButton*)[self.scroll viewWithTag:numberForIncrease];
            btn.enabled=NO;
            [btn setUserInteractionEnabled:NO];
        }
    }


}

2 个答案:

答案 0 :(得分:2)

您必须正确禁用它。 UIButton.enabled状态的docs

  

如果启用状态为NO,则控件忽略触摸事件和   子类可能会有不同的描述。

通过添加DisableButton来电,检查是否正在调用NSLog(),&amp;完全取消DisableButton方法:

dispatch_sync(dispatch_get_main_queue(), ^{
    self.view.userInteractionEnabled = NO;
    _btn_sync_outlet.enabled = NO;
    _btn_sync_outlet.userInteractionEnabled = NO;
});

这可能对您有所帮助:)。

答案 1 :(得分:0)

我不明白为什么你需要从0到2577循环以禁用你已经有iVar指向的按钮..?

为什么不直接关闭userInteraction到整个视图(或者如果对于包含所有按钮的containerView不可能)?

[self.view setUserInteractionEnabled: NO ];

或者说,为什么不在那里使用iVar呢?

{
BOOL _ignoreButtons;
}
-(void)buttonPressed:(id)sender{

   if (!_ignoreButtons){

_ignoreButtons = YES;
//do your time consuming job, possibly on bg thread

//on completion..
_ignoreButtons = NO;
}

}