tags:

views:

436

answers:

3

I have a view that I tell to update when its data changes. But sometimes the data changes when the view isn't visible. So how can I detect that the viewcontroller is visible and accepting events? I could add a boolean that is changed in viewWillAppear and viewWillDisappear, but it seems like there ought to be a way to detect the state directly...

Thanks, Gary

+1  A: 

if it's a tableview you can do something like:

[(UITableView *)[self view] reloadData];

in the viewController's viewWillAppear. I'm not sure, that's what you're asking but anyways.

tmadsen
A: 

You can probably check the view's window property:

- (BOOL)isVisible
{
  return view.window != NULL;
}

But i think maintaining a BOOL variable is better.

mfazekas
+5  A: 

Short Answer: If you are using a UINavigationController you can use it's visibleViewController so that you don't have to keep track of a separate boolean value.

Long Answer: Typically when I find myself calling reloadData inside of viewWillAppear, it is an indication that my View needs to observe some additional part of my Model. The problem with reloadData is that it is going to reload your entire table. Often times, you really only need to update a small part of the table. My strategy is normally to update only what I need to as it occurs regardless of whether or not a particular view is visible at that time. I know that is a pretty vague response...maybe some sample code of yours would help me be more specific...

Jonathan Arbogast
> Typically when I find myself calling reloadData inside of > viewWillAppear, it is an indication that my View needs to observe > some additional part of my ModelAgreed.
Roger Nolan
Yup, that's what I was looking for. The line is: 'if(self.navigationController.visibleViewController != self) {...}'. BTW, I used this in a UIViewController in a UITabBarController.
DrGary