I have an iPhone app (in XCode 4) that uses the standard Tab Bar Application project template (UITabBarController).
My first view has a UITableView that gets data from a JSON feed. Here’s where the controller loads that data:
#import "FirstViewController.h"
#import "SBJson.h"
#import "WheelRecord.h"
#import "WheelTableCell.h"
#import "UIImageView+WebCache.h"
@implementation FirstViewController
@synthesize wheels, wheelsTable;
- (NSMutableArray *) wheels {
if (!wheels) {
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wheelspotting.com/recent.json"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *wheelData = [parser objectWithString:json_string error:nil];
self.wheels = [NSMutableArray array];
for (NSDictionary *wheel in wheelData)
{
WheelRecord *currentWheel = [[[WheelRecord alloc] init] autorelease];
currentWheel.title = [wheel objectForKey:@"title"];
currentWheel.smallImage = [wheel objectForKey:@"small_image"];
currentWheel.mediumImage = [wheel objectForKey:@"medium_image"];
currentWheel.largeImage = [wheel objectForKey:@"large_image"];
[self.wheels addObject:currentWheel];
}
}
return wheels;
}
I’m trying to reload that data any time the user selects a different tab and then goes back to the first tab.
I’ve tried setting wheels to nil in viewDidLoad and viewWillAppear but when I switch tabs and come back to the first tab, it never updates (I make changes to the data to test).
I don’t believe that ViewDidLoad will be called again as all the view controllers will be present at all times. I would have thought that viewWillAppear should have been called though.
Silly question, but are you sure the data is not being updated? Perhaps it’s just your table view that’s not being updated with the new data? You are calling [yourTableView reloadData] somewhere from within your viewWillAppear or viewDidAppear method? Alternatively, pop a [yourTableView reloadData] just before the end of your if code, eg
Tab bar controllers can be awkward things to work with, I know I’ve personally spent many hours swearing at them. One thing to try, if you’ve subclassed UIViewController in any way, or are using a UITableViewController or UINavigationController make sure you’ve set the correct object type in the inspector for relevant tab – I’ve had no end of problems due to forgetting to do this in the past.