I have a custom data controller that I use for my application and within it I have some delegate methods I have set up. They all work but not when I want them too. When I call [dataController refreshData] I have a delegate method refreshDidStart and refreshDidFinishWithoutError. I allocate data controller, set the delegate to self and call refreshData in applicationDidFinishLaunching but the delegate methods aren’t refreshing the tableviews in the view controllers when the method is done being executed. But when I set up a refresh UIBarButtonItem to call [dataController refreshData], the refreshDidFinish delegate method is called and does what is is suppose to. I believe it has to do with the split view controller I am using. Below is the code:
App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
dataController = [[DataController alloc] init];
[dataController setDelegate:self];
[dataController refreshData];
}
MasterViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
dataController = [[DataController alloc] init];
[dataController setDelegate:self];
self.tableView.backgroundView = nil;
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];
}
- (void)refreshDataDidStart:(DataController *)view {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"Loading";
}
- (void)refreshDataDidFinishWithoutError:(DataController *)view {
[callsTableView reloadData];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
- (void)refreshDataDidFailWithError:(DataController *)view withError:(NSError *)error {
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSLog(@"%@", error.localizedDescription);
}
In your App Delegate, you are setting DataController’s delegate to be the App Delegate. So the delegate methods that get called need to be in your App Delegate class code.
In your MasterViewController, you’re setting the DataController’s delegate to be the MasterViewController, so the DataController’s delegate methods get called in the MasterViewController class.
You will need to add DataController delegate methods to your App Delegate code to triger refreshing the table view, perhaps by using NSNotificationCenter. Or perhaps it would be better to get the App Delegate to tell the MasterViewController to call refreshData on its DataController.