0
votes

I copied the code to show the activity indicator from this post. When I called hideActivityViewer nothing happens, and the activityView is still there, and the activityIndicator is still spinning. It is as if hideActivityViewer does not do anything to the activityView at all.

Here is the code I have modified

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

Update: I'm using the KVO to detect for change in variable and use it to call showActivityViewer. Turns out, showActivityViewer was called more than once, as a result there are multiple activityViewer on the screen, so when I remove one, the other is still there and I have no to reference to it. I solved this by checking if the activityView already exist, and if so don't create a new one.

2
How have you declared activityView?rdelmar
It could be because activityView becomes nil at some point before a call to hideActivityViewer. Lets say u call showActivityViewer two times, you have two activityViews exactly on top of each other and the first one will never be hidden if you call hideActivityViewer. Even after matching number of hideActivityViewer calls, or more.Rajiv
Where are you calling hideActivityViewer? Do you see the NSLog() results in the console?Justin
@Rajiv Your hypothesis is correct! I added a if statement to check if activityView exist before creating a new one and it works. Thanksharinsa
now, how do I mark this thing as answered.harinsa

2 Answers

0
votes

You may be calling ur method "hideActivityViewer" from asynchronous or thread completion blocks. If so, call ur method on mainthread i.e
[self performSelectorOnMainThread:@selector(hideActivityViewer) withObject:nil waitUntilDone:NO];

0
votes

My comment as an answer:

It could be because activityView becomes nil at some point before a call to hideActivityViewer. Lets say u call showActivityViewer two times consecutively, you have two activityViews exactly on top of each other and the first one will never be hidden if you call hideActivityViewer. Even after matching number of hideActivityViewer calls, or more.