从UIButton视图中删除活动指示器子视图

时间:2015-11-13 17:15:42

标签: ios objective-c uiview uibutton uiactivityindicatorview

我用几个按钮构建了一个界面。这些按钮从互联网上获取信息以获取其标题。我为视图中的每个按钮添加了一个UIActivityIndi​​cator,如下所示:

- (void) addActivityIndicator {
    UIButton * button;

    for (int i = 1; i <= 10; i++){
        button = (UIButton *)[self viewWithTag:i];
        NSLog(@"Button: %i",i);
        [button setTitle:@"" forState:UIControlStateNormal];
        button.enabled = NO;

         UIActivityIndicatorView * buttonSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [buttonSpinner startAnimating];
        buttonSpinner.tag = i;
        buttonSpinner.frame = button.bounds;
        [button addSubview:buttonSpinner];
    }

}

但是,我似乎无法访问个别指标来删除它们。

[(UIButton *)[self viewWithTag:tag] setTitle:[NSString stringWithFormat:@"%@",[self addTitle:i]] forState:UIControlStateNormal];
[(UIButton *)[self viewWithTag:tag] setEnabled:YES];
[(UIButton *)[self viewWithTag:tag] [buttonSpinner stopAnimating]];

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

    for (int i = 1; i <= 10; i++){
        button = (UIButton *)[self viewWithTag:i];
        NSLog(@"Button: %i",i);
        [button setTitle:@"" forState:UIControlStateNormal];
        button.enabled = NO;

         UIActivityIndicatorView * buttonSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [buttonSpinner startAnimating];
        buttonSpinner.tag = i*99;
        buttonSpinner.frame = button.bounds;
        [button addSubview:buttonSpinner];
    }

然后,获取UIActivityIndicatorView

[(UIActivityIndicatorView *)[(UIButton *)[self viewWithTag:tag] viewWithTag:tag] stopAnimating];

答案 1 :(得分:-1)

也许您可以尝试迭代子视图并将其删除?

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

UIActivityIndicatorView *toRemove;
for (UIView *view in button.subviews)
{
    if ([view isKindOfClass:[UIActivityIndicatorView class]])
    {
        toRemove = (UIActivityIndicatorView *)view;
        break;
    }
}
[toRemove removeFromSuperview];