1

I have two problems I am trying to solve, one is refreshing the tab controller itself and the other is refreshing the content of the tabcontroller.

The reason I wish to refresh the tab controller itself is that my application has a web call which returns a JSON which sets up the order of the tabs and also sets up the content of each tab. I have got it so that when you back out of the application and reenter the application the tab controller is refreshed by putting

exit(0)

in the AppDelegates ApplicationDidEnterBackground so that when the user backs out and in again the controller will be refreshed. In the applicationDidFinishLaunchingWithOptions, I have set up a web call which calls the JSON which is then used to set up the tab order. I know this is not a good way of doing this, but for the time being, its the only solution I can think of. How else can I refresh the TabController?

As for the tab content, it is refreshed using using this code

   becomeActiveObserver = [[NSNotificationCenter defaultCenter] 
                         addObserverForName:UIApplicationDidBecomeActiveNotification
                         object:nil 
                         queue: nil 
                         usingBlock:^(NSNotification *note){ 

                          [self refresh];
                          [self viewDidUnload];
                         }]; 

I have set this type of code up for each of the 5 tabs. This works very well, but the problem is the content only refreshes when the application is exited and accessed again. The web call will be periodic and I would like it when the web call is made that the content will refresh itself without me having to back out and in to the application again.

For testing purposes I have set up a button in my settings screen (settings screen is just a another view within one of my tabs) that when clicked with read JSON is stored in the iPhone directory which is different from the JSON retrieved from the web call (saves me having to go to the server and keep changing the JSON there). When this button is clicked, it should read this new JSON, update the content and then refresh the view. I have tested this and the JSON is being read and the data is being updated (I set up a button on each screen which would read out the JSON it is using to confirm this) but the view is not refreshing until I exit and enter the application again.

The temp code I have set up in the settings screen to read the JSON stored on the device is

-(IBAction)RefreshApp:(id)sender{     



NSFileManager *fileManager = [NSFileManager defaultManager];

     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"widgjson" ofType:@"json"];  
     NSData *myData = [NSData dataWithContentsOfFile:filePath];  
     NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];   

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex: 0];
        NSString *docFile = [docDir stringByAppendingPathComponent: @"json.txt"];
        [fileManager removeItemAtPath:docFile error:NULL];
        [responseString writeToFile:docFile atomically:NO encoding:NSUTF8StringEncoding

DashboardVC *db = [[DashboardVC alloc] init];
    [db refresh];
    [db viewDidUnload];

}

At the bottom you can see I tried to call refresh from one of the tabs to refresh one of the views do see if that worked, but no luck. The refresh code only seems to work when you call it from inside the class itself, I can't seem to get it to refresh when accessing it from another class.

When a web call is made, I want every single tab to be refreshed at once. Any ideas on how I would do this?

Would be very grateful if someone could point me in the right direction.

1
  • Don't put an "exit()" call anywhere in your code. Terminating your app this way (where the user isn't the one manually killing / quitting the app) is sure grounds for Apple to reject your app from the app store. May 18, 2012 at 7:53

1 Answer 1

2

UITabBarController's setViewControllers: animated: method is what you want to use here.

When you receive a notification where you want to re-order the tabs, use that function to pass along the various view controllers owned (and referred to) in your tab bar in an array which is in the order of how you want your tabs to appear.

4
  • I figured out how to get that method working at last lol, it reorders the tabs which is great, but it doesn't refresh the content. Does this method not do this?
    – AdamM
    May 18, 2012 at 8:42
  • Updating your view controllers is a separate thing from adding them to a tab bar, Adam. How would you normally tell your view controllers to update their content? Do you have a "refreshMyData" method or something else that you created to update their content? If you do have something that indicates you want your view controllers to update their content, why not enumerate through each one and call your custom update method for them at the same time as you're setting your tab bar's tab ordering? May 18, 2012 at 8:44
  • I have a refresh method set up in my DashboardVC which is my home tab, you can see that in the code I provided above and I have called refresh. Now I know both refresh and the viewDidload function are being called as I have text that prints out whenever either of these methods are called, but the content is not updating until I back out and in again. This is what I find most confusing. Is it because I am declaring a new instance of DashboardVC when calling DashboardVC *db = [[DashboardVC alloc] init] then calling refresh on that?
    – AdamM
    May 18, 2012 at 8:53
  • That might be it. But you have an array of ALL the view controllers in the tab bar (before and after you reset the order of them) and when you re-order the tab bar, that's a perfect time to call your "refresh" on each of them. May 18, 2012 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.