The main project I am doing for Photoblog is an application to show all the latest entries and such. So I call a web service using JSON and it gives me the data I need. Now there might be a few second delay in sending the request and getting the request back. Once I get the request back I open my new view. So one way to show the user that activity is going on is creating a activity indicator.
Apple has UIActivityIndicatorView which we will use. Now I want to put this in the top navigation bar but by default you cannot. So we have to set a UIBarButtonItem then use a custom view option. So here is the function I created to do this
// initing the loading view
CGRect frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
// initing the bar button
UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:loading];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;
This isn’t anything major. It will create the UIActivityIndicatorView with a width and height of 25. It will start is animating and make it fit into the frame we created.
Then I init a UIBarButtonItem and use a initWithCustomView method to set it to use our activity indicator view. I then release the indicator and set the click target to self. I then place that button item on the right side of the bar.
Now I do this right before I begin the process of sending a request and getting the response. The problem is that I never see the animation due to the request and responsive locking the main thread.
So when I have threads figured out we will do the JSON request in a thread so the main thread isn’t locked.