views:

756

answers:

3

Hi,

I have a UITabBar + UINavigationController application which often needs data from the internet. Sometimes it takes quite a while before it gets it, so I would like to show an activity indicator.

What I was trying is to add a activityView to my window in my applicationDidFinishLaunching method:

[window addSubview:tabBarController.view];
fullscreenLoadingView.hidden = YES;
[window addSubview:fullscreenLoadingView];

And then I add the application delegate as a observer to the default notification center:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullscreenLoading:) name:@"startFullscreenLoading" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopFullscreenLoading:) name:@"stopFullscreenLoading" object:nil];

and implement the methods:

- (void)startFullscreenLoading:(NSNotification *)notification {
    fullscreenLoadingView.hidden = NO;
}

- (void)stopFullscreenLoading:(NSNotification *)notification {
    fullscreenLoadingView.hidden = YES;
}

When I then use this directly in the applicationDidFinishLaunching method the loading indicator view shows upp as expected:

[[NSNotificationCenter defaultCenter] postNotificationName:@"startFullscreenLoading" object:self];

But when I use it from one of the navigation controllers the startFullscreenLoading: method is called but I don't see the loading indicator view. Why is that?

A: 

The most likely thing I can guess is that your indicator view is below some other view. Try

[[fullScreenLoadingView superView] bringSubviewToFront:fullScreenLoadingView]

If that doesn't work, I would suggest breaking inside of -startFullscreenLoading to make sure the fullscreenLoadingView is still valid.

Steven Canfield
I tried both, NSLog(@"%@", fullScreenLoadingView); says it is a <UIView: 0x1431730; frame = (0 0; 320 460); alpha = 0.246479; hidden = YES; autoresize = RM+BM; layer = <CALayer: 0x14317a0>>and [window subviews] contains both views.
Jeena
+3  A: 

Are you loading your data on a background thread? Your main thread will be blocked & won't be able to redraw or process any other events.

Ben Scheirman
Ok I understand, no I was loading it in the main thread, I thought that the main thread would first send the notification and tun the method and after that it would do the data loading. But ok, I see, I try to load it in an other thread.
Jeena
Thanks, that was my problem and it's fixed now :-)
Jeena
A: 

In my app I did this by adding the UIActivityView in IB and making sure it was above everything else (as a top-level object). It's set to be invisible when stopped.

Then in my code I make it appear with [activityIndicator startAnimating] and make it disappear with [activityIndicator stopAnimating];

Steve Weller