first of all: my question is very theoretical. Even if I post an example, I just want to know which implementation is the best one to solve this kind of problem. Maybe you will laugh when you read this question because it is very fundamentally – but I want to understand how to deal with such a situation.
Imagine the following: You have got an application which communicates with an extern API via XML. As a fact of this the view cannot appear immediately, because the API needs time to react. My idea was to implement a subview which contains a loading animation. When the API sends a response this subview is removed and the main view appears. Here is my example:
- (void)viewWillAppear:(BOOL)animated {
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[myView setBackgroundColor:[UIColor whiteColor]];
myView.tag = 1;
UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
loadingView.frame = CGRectMake(145, 160, 25, 25);
[myView addSubview:loadingView];
[self.view addSubview:myView];
[loadingView startAnimating];
}
- (void)viewDidAppear:(BOOL)animated
{
[self loadXMLData];
[self.tableView reloadData];
[[self.view viewWithTag:1] removeFromSuperview];
}
Everthing works fine. My problem is the following: it’s not only this view where I have to do that. My applications consists of many views, so what is the best way to avoid repeating this code? I thought about following: I modify the (UIKit UIViewController) viewDidAppear selector and put the code in it. I don’t know if Apple allows to change their frameworks. Furthermore it looks very dirty to me ;o) Is someone able to tell me how this is usually done? Thank you!
( hope you understand me, my first language is not English 🙁 )
Why not just make a subclass of
UIViewControllerwhich has this code implemented inviewWillAppearandviewDidAppear, and then inherit every other view controller in the application from this “base” subclass rather than UIViewController? That will be much easier than trying to do anything to UIViewController directly.