我复制了代码以显示这篇文章中的活动指示器。当我打电话hideActivityViewer
时没有任何反应,并且activityView
仍然存在,并且 activityIndicator 仍在旋转。好像hideActivityViewer
什么都没做activityView
。
这是我修改的代码
-(void)showActivityViewer
{
WBAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UIWindow *window = delegate.window;
_activityView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, window.bounds.size.width, window.bounds.size.height)];
_activityView.backgroundColor = [UIColor blackColor];
_activityView.alpha = 0.5;
UIActivityIndicatorView *activityWheel = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(window.bounds.size.width / 2 - 12, window.bounds.size.height / 2 - 12, 24, 24)];
activityWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
activityWheel.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[_activityView addSubview:activityWheel];
[window addSubview: _activityView];
[[[_activityView subviews] objectAtIndex:0] startAnimating];
}
-(void)hideActivityViewer
{
NSLog(@"Profile: hiding Activity Viewer");
[[[_activityView subviews] objectAtIndex:0] stopAnimating];
[_activityView removeFromSuperview];
_activityView = nil;
}
更新:
我正在使用 KVO 来检测变量的变化并使用它来调用showActivityViewer
. 原来,showActivityViewer
被调用了不止一次,结果activityViewer
屏幕上有多个,所以当我删除一个时,另一个仍然存在,我没有引用它。我通过检查是否activityView
已经存在来解决这个问题,如果存在,请不要创建新的。