For example, if a view of a viewController is in viewHierarchy and I just finished downloading stuff, I want to update stuff quickly. If self.view is not in viewHierarchy, I want to postpone updating.
I suppose, I can just add a boolean variable to indicate that and put it on viewWillAppear and viewWillDisappear.
I can also scan windows and see if UIView is in the viewHierarchy or not.
Is there a more straightforward way?
In the menu bar, select Window > Open Perspective, and then click Hierarchy View. You should see an arrangement similar to what's shown in figure 2. If not, select Window > Reset Perspective to return to the default layout.
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.
As of iOS 9.0, in Swift, you can do this in your view controller:
if viewIfLoaded?.window != nil {
// view should be updated
}
or this if you want to actually use the view:
if let view = viewIfLoaded, view.window != nil {
// update view
}
For older versions of iOS, in Objective-C:
if (self.isViewLoaded && self.view.window != nil) {
// view is in a view hierarchy and should be updated
}
viewDidLoad will get triggered only after the view loading is done, i think. So you can add necessary functionality in viewDidLoad itself, i think.
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