13

If I add a view as a subview like so

[self.view addSubview:mySubview];

Will there be called any method on mySubview, that I could override to add some custom behavior?

Pascal Klein
  • 23,665
  • 24
  • 82
  • 119

4 Answers4

28

Adding a view to a (new) superview triggers

- (void)willMoveToSuperview:(UIView *)newSuperview

and

- (void)didMoveToSuperview.

See the UIView Reference for more.

Till
  • 27,559
  • 13
  • 88
  • 122
3

You can override these two:

- (void)willMoveToSuperview:(UIView *)newSuperview
- (void)didMoveToSuperview

Take a look in the documentation for UIView for similar methods.

Tom Irving
  • 10,041
  • 6
  • 47
  • 63
3

Yes, There is a method which get called if one change the superview . you need to override the below method in your subview class.

- (void)willMoveToSuperview:(UIView *)newSuperview
- (void)didMoveToSuperview

From UIView Doucumentation

willMoveToSuperview:, didMoveToSuperview—Implement these methods as needed to track the movement of the current view in your view hierarchy.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

exep for special purpose is far better to customize you view in init phase, you have all you need and (more important) is a synchronous call.

ingconti
  • 10,876
  • 3
  • 61
  • 48