Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a specific UIViewController's view is currently visible? [duplicate]

Possible Duplicate:
How to tell if UIViewController's view is visible

I'm developing an app that processes a constant stream of incoming data from the network and provides a number of different UIViews for the user to view that data.

When certain model data gets updated based on the incoming stream from the network, I access the associated UIViewController or UITableViewController and do -setNeedsDisplay on it (in the case of UIViewController) or -reloadData (in the case of UITableViewController).

Is there a way to check if a given UIView is currently being displayed (beyond just being loaded) so that I only do -setNeedsDisplay or -reloadData if the user is currently looking at that UIView? It would seem that calling -setNeedsDisplay or reloadData on a view that the user is not currently looking at is a waste of processing power and wouldn't be good for battery life. When the user eventually switches over to a view that previously got updated, doing -setNeedsDisplay or reloadData on the -viewWillAppear would make more sense.

Thanks

like image 651
Paul Avatar asked Sep 09 '10 15:09

Paul


People also ask

How do I know if a ViewController is visible?

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller: Invoking the view method causes the view to load (if it is not loaded) which is unnecessary and may be undesirable. It would be better to check first to see if it is already loaded.

How do you check if a view contains a Subview?

If you need a quick way to get hold of a view inside a complicated view hierarchy, you're looking for viewWithTag() – give it the tag to find and a view to search from, and this method will search all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number.

How many times does viewDidLoad get called?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.


2 Answers

After doing some research, I found this answer in a different question posted on here...This seems to be the best way...

The view's window property is non-nil if a view is currently visible, so check the main view in the view controller:

if (viewController.isViewLoaded && viewController.view.window){     // viewController is visible } 
like image 136
Paul Avatar answered Oct 12 '22 22:10

Paul


Add this to your controllers, or to a subclass of UIViewController that you can then subclass further. Access it using a property or the variable:

- (void)viewDidAppear:(BOOL)animated {  [super viewDidAppear:animated];  visible = YES; }  - (void)viewWillDisappear:(BOOL)animated {  visible = NO;  [super viewWillDisappear:animated]; } 
like image 43
Peter DeWeese Avatar answered Oct 12 '22 21:10

Peter DeWeese