iOS viewDidLoad for UIView

IosUiviewUiviewcontroller

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

If you load it from a XIB file, the awakeFromNib method will be called once loading finishes:

override public func awakeFromNib() {
    super.awakeFromNib();

    // ... loading logic here ...
}

Update; 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's viewDidLoad method if possible.

Solution 2 - Ios

You can use willMove(toSuperview newSuperview: UIView?)

import UIKit

final class myView: UIView {

  override func willMove(toSuperview newSuperview: UIView?) {
     super.willMove(toSuperview: newSuperview)
     //Do stuff here
   }

} 

Apple Docs

Solution 3 - Ios

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:

- (void)viewDidLoad{
    [super viewDidLoad];
    
    // init your parameters here
}

Analogously, in your view's init method:

- (id)initWithDelegate:(id)_delegate
{
    self = [[[[NSBundle mainBundle] loadNibNamed:@"YourView" owner:self options:nil] objectAtIndex:0] retain];
    if (self) {
        [super init];
    
        self.delegate = _delegate;

        // init your parameters here

        return self;
    
    }
    return nil;
}

Then, you create YourView from view controller like this:

YourView view = [[YourView alloc] initWithDelegate:self];
[self.view addSubview:view];
[view release];

Further, things that you want to do when your view did load, you can place in layoutSubviews method in your view, like this:

-(void)layoutSubviews{
    [super layoutSubviews];

    // init your parameters here, like set up fonts, colors, etc...
}

I think, that is what you need.

Cheers!

Solution 4 - Ios

Swift 2:

import UIKit

class myView: UIView {
  
  override func layoutSubviews() {
    print("run when UIView appears on screen")
    // you can update your label's text or etc.
  }
}

Solution 5 - Ios

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...

//  UIViewController
- (void)viewDidLoad
{

    [super viewDidLoad];

    [self.view viewDidLoad];
}


//  UIView+Loading.m
#import < UIKit/UIKit.h>

@implementation UIView (Loading)

- (void)viewDidLoad
{

    for (UIView* subview in self.subviews)

        [subview viewDidLoad];
}

@end


//  UIView+Loading.h
#import < UIKit/UIKit.h>

@interface UIView (Loading)

- (void)viewDidLoad;

@end


// UIView_CustomImplementation

- (void)viewDidLoad
{

	NSLog(@"Do whatever you want to do in custom UIView after method viewDidLoad on UIViewController was called");

}

Solution 6 - Ios

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, the viewDidLoad method is not always called.

Solution 7 - Ios

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:

viewController.view;

The view controller then calls:

- (void)loadView; // This is where you put your own loading view information, and set the viewController's self.view property here.

Followed by:

- (void)viewDidLoad;

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:

- (void)viewWillUnload;
// view unloaded and set to nil.
- (void)viewDidUnload;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmanuelBetancurtView Question on Stackoverflow
Solution 1 - IosborrrdenView Answer on Stackoverflow
Solution 2 - IosdavidrynnView Answer on Stackoverflow
Solution 3 - IosakelecView Answer on Stackoverflow
Solution 4 - IosfatihyildizhanView Answer on Stackoverflow
Solution 5 - IosvedranoView Answer on Stackoverflow
Solution 6 - IosTranzView Answer on Stackoverflow
Solution 7 - IosthebarcodeprojectView Answer on Stackoverflow