我在这里阅读了各种关于在UIButton中添加UIActivtyIndicatorView的帖子,但到目前为止还没有一个帮助过我。
我有一个带有单元格的tableview,每个单元格中都有一个按钮,表示“go”。 当用户单击按钮时,我希望按钮显示UIActivtyIndicatorView,直到完成所有后台操作。这里我使用一些解析方法在后台保存用户信息。
我有一个方法
- (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];
}
正如您所看到的,这两种方法是我将按钮从普通颜色切换为突出显示的颜色,但在按钮突出显示之前,我希望显示UIActivityIndicatorView。
目前它没有显示,按钮只是在不显示ActivtyIndicator的情况下切换颜色。
感谢任何帮助。谢谢!
编辑:更改了UIActivityView init行,以便我现在传入button.bounds属性。
ActivtyIndicatorView现在正在显示,但现在我的问题是重新加载圈显示在“GO”按钮标题下,然后在标题更改时停止。我想要它,以便在ActivityIndicator显示时按钮标题消失。有什么建议吗?
答案 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视图中,成为UIButton
,UITextView
或其他。
为什么不把你的UIActivityIndicatorView
放在上面呢?这可以保证有效。
UIActivityIndicatorView * spin = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spin.center = myButton.center;
[myButton.superview insertSubview:spin aboveSubview:myButton];
[spin startAnimating];