In a ViewController, there is ViewDidLoad
to know when the VC has loaded.
For a UIView, what method do i have to use when the view loaded?
Would this method be called with any init?
edit: No XIB, just programmatically.
In a ViewController, there is ViewDidLoad
to know when the VC has loaded.
For a UIView, what method do i have to use when the view loaded?
Would this method be called with any init?
edit: No XIB, just programmatically.
If you load it from a XIB file, the awakeFromNib method will be called when it is loaded from the XIB.
edit In the case of no XIB, you will probably have to infer it using one of the methods from the Observing View-Related Changes area of the docs (for example, didMoveToSuperview). However, a better way is to send a message to your views from the view controller in the viewDidLoad method.
I had similar problem and found relative easy solution. Idea is to send viewDidLoad to every child view at right time and overload that method on class of your interest.
To do that add this parts of code in your classes...
It doesn't quite work like this.
- (void)viewDidLoad
is not called when the view controller is loaded; it is called when the view controller's view is loaded.So you can create the UIViewController, and the only methods that will be called are the init methods used to initialise it. The view will not be created, the -(void)viewDidLoad method is not called, etc.
Then when something else asks the view controller for its view, via:
The view controller then calls:
Followed by:
View Did Load is a separate method so you don't have to interrupt the actual loadView method, and the complex view loading options. Subclassing and overriding the loadView method when using nibs etc can result in problems when developers aren't sure what Apple is doing and what their best practices are, so it was smart for Apple to separate the method out.
Then, when a memory warning comes the view is released, and set to nil:
You can use willMove(toSuperview newSuperview: UIView?)
Apple Docs
As far as I know, I don't think there's such a method except for the init. Usually I do the preparation in the init method. You may create your own
viewDidLoad
method and call it manually. But most of time, UIView is managed by it's view controller, so that view controller should know when the view is loaded, if you want to config the view, you may do it in the view controller. By the way, theviewDidLoad
method is not always called.Actualy, you have not to do anything with view controller's method viewDidLoad(), for your view's initialization. All that you want to do, you can do in view's init method. For example, in view controller's viewDidLoad(), there is some initialization code:
Analogously, in your view's init method:
Then, you create YourView from view controller like this:
Further, things that you want to do when your view did load, you can place in layoutSubviews method in your view, like this:
I think, that is what you need.
Cheers!