3

I have a problem regarding reload of view.

I use tabbar in my application. When I press home button and my app is in background. Now my problem is start now. I Want that every time my first tab is display. So, every time I get 1st tabbar's view. But When I get 1st tabbar's view, I call viewDidAppear() of 1st tabar's viewController in AppDelegate like below:

- (void)applicationDidBecomeActive:(UIApplication *)application
{

     self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:0];
     [navigationController1 popToRootViewControllerAnimated:YES];
     [navigationController2 popToRootViewControllerAnimated:YES];
     [navigationController3 popToRootViewControllerAnimated:YES];
     [navigationController4 popToRootViewControllerAnimated:YES];
     [navigationController5 popToRootViewControllerAnimated:YES];

     simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil];
     [s viewDidAppear:YES];
}

So,viewDidAppear() is called and data is loaded and log is printed. But view is not reloaded or refreshed. Means there is not any effect in tableview. Data which is loaded from server is displayed in tableview.

I use the following code for reload or refresh view:

        [self.view setNeedsLayout];
        [self.view setNeedsDisplay];
        [tabledata reloadData];

But it is not effected.

Thanks in advance.

4

2 回答 2

19

When you're calling:

simpleTableViewController *s = [[simpleTableViewController alloc]initWithNibName:@"simpleTableViewController" bundle:nil];
[s viewDidAppear:YES];

You are simply creating a new instance of that view controller and attempting asking it to refresh it's data. Not the view controller that is already in the navigation stack.

I suggest that you add your simpleTableViewController as an observer for the "application did become active" or "will enter foreground" notification. E.g.:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

Then in the notification handler you can reload your data:

- (void)applicationWillEnterForeground:(NSNotification *)notification {
    [self reloadDataFromWeb];
    [self.tableView reloadData];
}

Don't forget to remove the observer in the simpleTableViewController's dealloc method.

于 2013-04-12T12:51:38.057 回答
0

Do not call viewDidAppear. Show your view controller and iOS will call your viewDidAppear for you when the view truly appears.

于 2013-04-12T12:43:40.443 回答