PDA

View Full Version : Holding off pushing a new view until the activity indicator appears?




neiltc13
Feb 24, 2009, 11:17 AM
I have a pretty simple application design - a table view with a list of screenshot names. When you tap one it loads a new view where the image loads.

I want the images to download from the web and I have managed it. However, when the user selects a table cell, the new view does not appear until all of the image data is downloaded.

So, I wanted to put an activity indicator on screen. I've managed to get it to work, but the problem is that it only appears just as the view is animating to the next view. So, really, it seems like it too is waiting for the data in the new view to download before displaying.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[activityIndicator startAnimating];
BMWAppDelegate *appDelegate = (BMWAppDelegate *)[[UIApplication sharedApplication]delegate];
Shots *shots = (Shots *)[appDelegate.shots objectAtIndex:indexPath.row];

//------Below if statement may be required for memory management but removed to get a new view to load
//------each time and thus load the image that the user selects from the table
//if (self.shotView == nil){
ShotViewController *viewController = [[ShotViewController alloc] initWithNibName:@"ShotViewController" bundle:[NSBundle mainBundle]];
self.shotView = viewController;

self.shotView.hidesBottomBarWhenPushed = YES; //Hide the tab bar when viewing an image
self.shotView.imgPath = [shots imgloc]; //Pass the URL of the image from the array to the view controller
[viewController release];


//}

[self.navigationController pushViewController:self.shotView animated:YES];
//self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.shotView.title = [shots name];
[self.shotView.shotDescription setText:[shots description]];
}

All I really need to know is how to display the activity indicator before proceeding with the rest and loading the new view. It's driving me up the wall, I didn't expect this problem when I started out and the things I thought would be difficult are turning out to be easy :p



jnic
Feb 24, 2009, 11:26 AM
I want the images to download from the web and I have managed it. However, when the user selects a table cell, the new view does not appear until all of the image data is downloaded.

This is because (presumably) you're doing the data fetch in the main thread, which also happens to be the one used for UI updates. This means that while the fetch is running, the UI thread is blocked from doing updates, so your spinner doesn't appear until it's done.

The solution is to perform your data fetch in another thread, and call back the main thread when you're done. Take a look at performSelectorInBackground and performSelectorOnMainThread respectively (both in NSObject (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html)). You'll need to use the latter to make any UI updates from other threads or they'll be ignored.

neiltc13
Feb 24, 2009, 12:07 PM
Thanks for the pointer. I found this page:
http://iphone.zcentric.com/2008/08/28/using-threads-for-a-json-request/

And my application now does what I want it to! :D

flaminfish
Feb 12, 2010, 12:23 AM
neiltc13, I'm working with very similar code, and was curious as to how you got your activity indicator implemented. (I'm assuming you got it to work as is evident of your smiley)