I’m using a recipe from the iOS Recipes PragProg book, here’s the original portion of code I’m using:
- (void)addActivityIndicatorView {
activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;
CGRect aiFrame = activityIndicatorView.frame;
CGFloat originX = (self.view.bounds.size.width - aiFrame.size.width) / 2;
CGFloat originY = (self.view.bounds.size.height - aiFrame.size.height) / 2;
aiFrame.origin.x = floorl(originX);
aiFrame.origin.y = floorl(originY);
activityIndicatorView.frame = aiFrame;
[self.view addSubview:activityIndicatorView];
}
I realized I wanted this activityindicator code for multiple activityIndicatorViews on my other controls that load data via the web, so this was my implementation to make reusable code and it is not showing an activityIndicator view over the control like the above code does for the one viewcontroller it’s in. Yes I do step into this code for each control that uses it, and I call [activityIndicator startAnimating]; for the activityIndicator that I pass into this function. Guess what I’m asking is where should I look at elsewhere in the code?
- (void)addActivityIndicatorView:(UIActivityIndicatorView *)acitivityIndicator toView:(UIView *)view {
acitivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
acitivityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;
CGRect aiFrame = acitivityIndicator.frame;
CGFloat originX = (view.bounds.size.width - aiFrame.size.width) / 2;
CGFloat originY = (view.bounds.size.height - aiFrame.size.height) / 2;
aiFrame.origin.x = floorl(originX);
aiFrame.origin.y = floorl(originY);
acitivityIndicator.frame = aiFrame;
[view addSubview:acitivityIndicator];
}
Write your method like this:
When you want to use the above method, pass the reference to a
UIActivityIndicatorView *to the method for example:Now whenever you want it to start animating: