开发者

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

This question already has answers here: Closed 10 years ago.

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


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];
}


Just for completeness, I thought I'd add in how to determine if the view controller is being displayed in a tab based app:

+(BOOL) isSelectedViewController:(UIViewController *)someVC;
{
    myAppDelegate   *appD = [[UIApplication sharedApplication] delegate];
    UIViewController *selectedVC = [appD.TabBarController selectedViewController];

    return selectedVC == someVC;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜