Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView - How to get notified when the view is loaded?

Is there anything similar to the viewDidLoad of UIViewController for a UIView??? I need to be notified as soon as a UIView has loaded (Subclass of UIView), and perform some actions.

like image 794
aryaxt Avatar asked Dec 21 '10 17:12

aryaxt


People also ask

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.


1 Answers

Depending on what kind of actions you need to perform, there are several techniques:

  1. -(id)initWithFrame:(CGRect)frame - UIView's designated initializer; always sent to a UIView to initialize it, unless the view is loaded from a nib;
  2. -(id)initWithCoder:(NSCoder *)coder - always sent to initialize a UIView whenever the view is loaded from a nib;
  3. -(void)awakeFromNib - sent after all the objects in the nib are initialized and connected; applicable only if you load the object from a nib; you must call super;
  4. -(void)willMoveToSuperview:(UIView *)newSuperview - sent immediately before the view is added as a subview to another view; newSuperview may be nil when you remove the view from its superview;
  5. -(void)willMoveToWindow:(UIWindow *)newWindow - sent immediately before the view (or its superview) is added to a window; newWindow may be nil when you remove the view from a window;
  6. -(void)didMoveToSuperview - sent immediately after the view is inserted into a view hierarchy;
  7. -(void)didMoveToWindow - sent immediately after the view gets its window property set. -

Basically, you can choose to perform your actions during initialization (1 & 2), after loading from a nib (3), before insertion into a view hierarchy (4 & 5) and after that (6 & 7).

like image 193
Costique Avatar answered Sep 20 '22 11:09

Costique