Friday 19 July 2013

Check view controller is visible or not. UIViewController and UINavigationController


if (viewController.isViewLoaded && viewController.view.window)
    // viewController is visible
 else  // viewController is not visible


UIViewController


1.) isViewLoaded

Returns a Boolean value indicating whether the view is currently loaded into memory.
- (BOOL)isViewLoaded
Return Value
A Boolean value indicating whether the view is currently loaded into memory.
Discussion
Calling this method reports whether the view is loaded. Unlike the view property, it does not attempt to load the view if it is not already in memory.

2.) viewController.view.window

If  view  is currently visible the its window property is non-nil. So we check viewController.view.window.
Calling the view method loads the view(if not yet loaded), so we call isViewLoaded before it.

UINavigationController 


In UINavigationController we can use its visibleViewController property.

visibleViewController

The view controller associated with the currently visible view in the navigation interface. (read-only)
@property(nonatomic, readonly, retain) UIViewController *visibleViewController
Discussion
The currently visible view can belong either to the view controller at the top of the navigation stack or to a view controller that was presented modally.
We can compare this with our viewController and find whether its visible or not.

5 comments: