在UIButton中添加UIActivityIndi​​catorView

时间:2014-07-13 03:30:53

标签: ios objective-c uibutton uiactivityindicatorview

我在这里阅读了各种关于在UIButton中添加UIActivtyIndi​​catorView的帖子,但到目前为止还没有一个帮助过我。

我有一个带有单元格的tableview,每个单元格中都有一个按钮,表示“go”。 当用户单击按钮时,我希望按钮显示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 init行,以便我现在传入button.bounds属性。

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

2 个答案:

答案 0 :(得分:2)

- (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;
}

希望这会对你有所帮助。

答案 1 :(得分:1)

为什么要经历这么多麻烦并尝试将UIActivityIndicatorView嵌入到另一个Cocoa视图中,成为UIButtonUITextView或其他。

为什么不把你的UIActivityIndicatorView放在上面呢?这可以保证有效。

UIActivityIndicatorView * spin = [[UIActivityIndicatorView alloc]
    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spin.center = myButton.center;
[myButton.superview insertSubview:spin aboveSubview:myButton];
[spin startAnimating];