this is how we make UIActivityIndicatorView work #iphone #sdk #objective-c (tiny tutorial)
To begin with - I think it's necessary to mention that I am not an expert in Objective-C, neither in iPhone SDK. I had some experience developing for mobile devices (Siemens, to be precise) like 8 years ago, and that was fun, but obviously it was nothing comparing to what apps nowadays can do.
@interface RootViewController : UIViewController {
UIActivityIndicatorView *cLoadingView;
}
@property (nonatomic, retain) UIActivityIndicatorView *cLoadingView;
- (void)initSpinner;
- (void)spinBegin;
- (void)spinEnd;
@end
...
Simplest case possible. We have three methods defined here. One is for initialization, and we will call it when controller will just load; other two to start and stop spinning. Easy, so our controller's .m file will look like this (again, on http://pastie.org/869680):
...
@implementation RootViewController
...
@synthesize cLoadingView;
...
- (IBAction)doSomethingComplexAndTimeConsuming {
// this is how we start spinning
[NSThread detachNewThreadSelector: @selector(spinBegin) toTarget:self withObject:nil];
...
// TODO: something very complex and time consuming
// you can also add extra wait for the test (just uncomment line below)
//[NSThread sleepForTimeInterval:3];
...
// stop waiting...
[NSThread detachNewThreadSelector: @selector(spinEnd) toTarget:self withObject:nil];
...
// TODO: continue with our application
}
...
- (void)initSpinner {
cLoadingView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
// we put our spinning "thing" right in the center of the current view
CGPoint newCenter = (CGPoint) [self.view center];
cLoadingView.center = newCenter;
[self.view addSubview:cLoadingView];
}
...
- (void)spinBegin {
[cLoadingView startAnimating];
}
...
- (void)spinEnd {
[cLoadingView stopAnimating];
}
...
- (void)viewDidLoad {
[self initSpinner];
[super viewDidLoad];
}
...
@end
...
Simple, isn't it? That really is trivial, and I thought most of the cases can be handled the same way. Unfortunately, when I wanted it to works fancier (I wanted the spinning gear to be in the navigation bar of my app) -- it brought me confusion on how this can be done. Just positioning up there? That's a pain, and I never was able to make it work (frankly speaking :-). However, then, I realized other thing. Navigation bar has buttons! So, why wouldn't we make our spinning gear a button? and this is what I did. It was really easy to do -- just tweaking our initSpinner method a little bit (on http://pastie.org/869703):
...
- (void)initSpinner {
cLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityButton = [[UIBarButtonItem alloc] initWithCustomView: cLoadingView];
self.navigationItem.rightBarButtonItem = activityButton;
}
...
Simple. It's all looks pretty simple when there is a working example, isn't it? :-)
To crown it all, I would love to mention that Apple actually worked hard on making deep documentation available. And while they certainly limit developer with UI requirements and restrictions, they allow to build beautifully helpful and looking application very fast.
