Reputation: 1960
I am trying to create methods that I can reuse to start and stop spinning wheels during different lengthy activities such as syncing a table with a server.
My start method:
-(void) startSpinner {
UIActivityIndicatorView *activityIndicator;
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
[activityIndicator startAnimating];
}
and end method
-(void)endSpinner:(UIActivityIndicatorView *) spinner forTable:(UITableView *)tableView {
[tableView reloadData];
[spinner stopAnimating];
tableView.userInteractionEnabled = YES;
}
The problem I'm running into is the end method does not recognize the uiactivityidicator created in the start method.
Should I be saving this in a property? Or how can I grab the spinner from a different method in order to save it.
I'd like to set this up in reusable code as I have many tableviews in different view controllers where I would like to include this code. The tableviews already have properties but do I have to set up a uiactivityindicator property in every view controller where I want to include a spinner?
Thanks for any insights.
Upvotes: 1
Views: 1132
Reputation: 11201
Declare a property in AppDelegate:
@property (strong,nonatomic) MyActivityIndicator *activity;
and initialise it:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_activity=[[MyActivityIndicator alloc]init];
}
and then declare the following methods in appDelegate:
- (void)showActivity
{
dispatch_async(dispatch_get_main_queue(), ^{
[_window addSubview:_activity];
[_activity startAnimating];
});
}
- (void)hideActivity
{
dispatch_async(dispatch_get_main_queue(), ^{
//also remove activity from window
[_activity stopAnimating];
});
}
you can call these two methods from any class:
[(AppDelegate*)[UIApplication sharedApplication].delegate showActivity];
Upvotes: 2