connecting an UIActivityIndicator to a UIButton

android-activity, cocoa-touch, indicator

Solution

If you want to add an ActivityIndicator directly to your button, here's some code to do this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0,0,40,40); // increase width value if you want to add text   
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"location.png"] forState:UIControlStateNormal];

UIActivityIndicatorView *actIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
actIndicator.tag = 123456;
[actIndicator startAnimating];
actIndicator.frame = CGRectMake(7, 7, 26, 26);
[button addSubview:actIndicator];
[actIndicator release];

This shows the activity view, but you could either hide it in the beginning or toggle the activity indicator by accessing the indicator via its tag.

Problem

New to all this Xcode stuff - I have a few UIButton's that links to movie & music files from my web site - they works fine, it's just that there's some lag time while it's loading the MoviePlayer where it seems as if nothing's happening. I would like to set up an UIActivityIndicator - I can't sem to find a simple way to do it. And if so, would I need one for each button? Could you help? Thanks Carl

Original source