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
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.
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.
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.
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 }
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]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With