我在这里阅读了有关在UIButton中添加UIActivtyIndi​​catorView的各种文章,但到目前为止,这些都没有帮助我。

我有一个带有单元格的表格视图,每个单元格中都有一个说“去”的按钮。
当用户单击一个按钮时,我希望该按钮显示一个UIActivtyIndi​​catorView,直到完成所有背景工作为止。在这里,我使用一些解析方法将用户信息保存在后台。

我有办法

- (void)going:(UIButton *)button {

    NSLog(@"going button pressed");

    //Added this code
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithFrame:button.bounds];
    [button addSubview:activity];
    [activity startAnimating];

    if ([button.titleLabel.text isEqualToString:@"Go"]) { // Select
     ....
     // Just code changing user info

        [PFObject saveAllInBackground:save block:^(BOOL success, NSError *error) {

            if (success) {

                //Do more stuff so save user info in background
            }
        }];
    }
    else { // Deselect

        //More stuff
            if (success) {

                //Toggle button
                [defaults setObject:@"" forKey:@"currentBarID"];
                [defaults synchronize];
                selectedGoingToButton = nil;
                [self deselectGoingButton:button];
                [self reloadData];
            }
        }];
    }
}
- (void)selectGoingButton:(UIButton *)button {



    NSLog(@"GoingButtonselected");
    [button setTitle:@"Going" forState:UIControlStateNormal];
    button.layer.borderColor = neonBlue.CGColor;
    [button setNeedsDisplay];
}

如您所见,这两个方法是我将按钮从纯色切换为突出显示的颜色,但是在按钮突出显示之前,我希望显示UIActivityIndi​​catorView。

当前它不显示,并且按钮仅切换颜色而不显示ActivtyIndi​​cator。

任何帮助表示赞赏。谢谢!

编辑:更改了UIActivityView初始化行,以便我现在传递button.bounds属性。

ActivtyIndi​​catorView现在正在显示,但是现在我的问题是重新加载圆圈显示在“GO”按钮标题下,然后在标题更改时停止。我想要它,以便在显示ActivityIndi​​cator时按钮标题消失。有什么建议?

最佳答案

- (void)selectGoingButton:(UIButton *)button
{

    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
    activityIndicator.backgroundColor = [UIColor clearColor];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [activityIndicator startAnimating];
    [yourview addSubview:activityIndicator];


    NSLog(@"GoingButtonselected");
   [button setTitle:@"Going" forState:UIControlStateNormal];
    button.layer.borderColor = neonBlue.CGColor;
   [button setNeedsDisplay];
}







- (void)going:(UIButton *)button
{

    NSLog(@"going button pressed");

    //Added this code
//UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithFrame:button.bounds];
//[button addSubview:activity];
//[activity startAnimating];

   if ([button.titleLabel.text isEqualToString:@"Go"]) { // Select
   ....
   // Just code changing user info

       [PFObject saveAllInBackground:save block:^(BOOL success, NSError *error) {

           if (success) {

               //Do more stuff so save user info in background
           }
       }];
   }
   else { // Deselect

       //More stuff
           if (success) {

               //Toggle button
               [defaults setObject:@"" forKey:@"currentBarID"];
               [defaults synchronize];
               selectedGoingToButton = nil;
               [self deselectGoingButton:button];
               [self reloadData];
           }
       }];
   }

[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}

希望对您有帮助。

关于ios - 在UIButton中添加UIActivityIndi​​catorView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24719290/

10-13 06:34