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

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

Comments

0

You can probably check the view's window property:

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

But i think maintaining a BOOL variable is better.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.