UIActivityIndicatorView for UITableViewController in iPhone

Generally, it is required to show activity indicator view during asynchronous/time consuming processes. For example, In webservice application, you have to wait a few seconds to make the request and get the response mean time UIActivityIndicatorView is displayed to indicate the processing is going on. Suppose you require showing UIActivityIndicatorView in UITableViewController. It might be on cell click or on loading. We need to disable the form controls on the form during activity and show activity indicator view.

See following code for this:
// new transparent view to disable user interaction during uploading.
    UIView *loadView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    //Loader spinner
    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [loadView addSubview:act]; 
    act.center =loadView.center;
    [self.view addSubview:loadView];
    [self.view bringSubviewToFront:loadView];
    [act startAnimating];
    [act release];
    [loadView release];
   [NSThread detachNewThreadSelector:@selector(doWork) toTarget:self withObject:nil];
Here is doWork method which contains all time consuming task. It is executed in separate thread so you can’t access UI elements in this method.

- (void) doWork {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

.....Time Consuming Code here .....

[pool release];


Hope, It helps.

No comments:

Post a Comment