How to check if a specific view of a UIViewController is currently being displayed?

Possible duplicate:
How to find out if a UIViewController view is visible

I am developing an application that processes a constant stream of incoming data from the network and provides the user with various types of UIView for viewing this data.

When some model data is updated based on an incoming stream from the network, I refer to the corresponding 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 (except that it is loading), so that I only do -setNeedsDisplay or -reloadData if the user is currently looking at this UIView? It would seem that calling -setNeedsDisplay or reloadData in a view that the user is not currently looking at is a waste of computing power and would not be useful for battery life. When the user eventually switches to a view that was previously updated, doing -setNeedsDisplay or reloadData in -viewWillAppear will make more sense.

thank

+45
objective-c iphone
Sep 09 '10 at 15:45
source share
3 answers

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

The viewport property is non-zero if the view is currently visible, so check the main view in the view controller:

if (viewController.isViewLoaded && viewController.view.window){ // viewController is visible } 
+131
Sep 09 '10 at 22:41
source share

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

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; visible = YES; } - (void)viewWillDisappear:(BOOL)animated { visible = NO; [super viewWillDisappear:animated]; } 
+12
Sep 09 '10 at 15:53
source share

Just for completeness, I thought I would add how to determine if the view controller is displayed in a tab-based application:

 +(BOOL) isSelectedViewController:(UIViewController *)someVC; { myAppDelegate *appD = [[UIApplication sharedApplication] delegate]; UIViewController *selectedVC = [appD.TabBarController selectedViewController]; return selectedVC == someVC; } 
-four
Jun 16 '11 at 19:32
source share



All Articles