DrGary
DrGary

Reputation: 3321

How can I tell if my viewcontroller is visible and accepting events?

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

Upvotes: 3

Views: 2723

Answers (3)

Jonathan Arbogast
Jonathan Arbogast

Reputation: 9650

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...

Upvotes: 12

mfazekas
mfazekas

Reputation: 5699

You can probably check the view's window property:

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

But i think maintaining a BOOL variable is better.

Upvotes: 0

tmadsen
tmadsen

Reputation: 951

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.

Upvotes: 1

Related Questions