iPhone refreshing tabcontroller and content

北城以北 提交于 2020-01-15 23:07:55

问题


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:


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.



来源:https://stackoverflow.com/questions/10648696/iphone-refreshing-tabcontroller-and-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!