views:

52

answers:

1

I'm trying to create an activity indicator in iPhone app. The problem is that I cannot get it to appear before the actual task i want it to diplay during is already done. Is there something funky about the order in which the iPhone does stuff?

Here is my problematic code (in my app delegate):

-(BOOL)showProgressView: (NSString *) message { 
self.progress = [[UIView alloc] initWithFrame:window.frame];

UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage    imageNamed:@"squircle.png"]];
[img setAlpha:0.5];
[img setFrame:CGRectMake(94, 173, 133, 133)];


UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(51.5, 51.5, 30, 30)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

[img addSubview:spinner];

[self.progress addSubview:img];


[spinner startAnimating];
[img release];
[spinner release];

[window addSubview:self.progress];

return YES;

}

I then call this code like this:

if ([appDelegate showProgressView:@"Loading..:"])
{

    //My actual code loads data and stuff here but that is not important
            //drawCtrl is a UIViewController subclass that is instantiated here 


    UINavigationController *navController = [appDelegate navigationController];
    [navController pushViewController:drawCtrl animated:YES];


    [drawCtrl release];
}

The problem is that my activity indicator does not appear until the new view controller is pushed onto the navController's stack. Can I control this in some way?

Thanks in advance!

-Mats

+1  A: 

You need to call your loading method periodically via a timer. UI changes require a trip through the event loop to be seen. I have done this usually in a timer I set up in the AppDelegate, it calls the "methodThatTakesSomeTime" every n seconds, the "methodThatTakesSomeTime" method does some work for a specified time slice and then exits which is usually about the same as the timer trigger time. This gives the event loop time to refresh the UI and also give your method time to do its stuff.

Takes some fiddling with keeping the state of the "methodThatTakesSomeTime" so it can continue on its way, but that is what it takes.

Kenny