setNetworkActivityIndicatorVisible spinner not displaying
As the title says, here's some code:
- (void)refreshMap {
NSLog(@"refreshing");
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
lat = [[NSNumber numberWithDouble:myUserLocation.coordinate.la开发者_开发知识库titude] stringValue];
lon = [[NSNumber numberWithDouble:myUserLocation.coordinate.longitude] stringValue];
NSString *url = [[NSString alloc] initWithFormat:@"http://mywebsite.com/geo_search.json?lat=%@&lon=%@", lat, lon];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[url release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
The network activity indicator (in the status bar) doesn't appear at all (not in the Simulator, nor on a real device). How do I solve this?
The UI won't be updated unless your code returns control to the runloop. So if you enable and disable the network indicator in the same method, it will never actually show. The solution is to call
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
in your url connection delegate methods (conectionDidFinishLoading:
and connection:didFailWithError:
).
精彩评论