iOS viewDidLoad for UIView
Asked Answered
S

7

73

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.

Seften answered 16/4, 2012 at 1:21 Comment(2)
Depends on where you loaded it from...Colocynth
hi thanks, Im loading the view from a viewControllerSeften
C
59

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.

Colocynth answered 16/4, 2012 at 1:26 Comment(3)
hi thanks will edit my question , no XIB, just programaticallySeften
Does it fire again if the UIView is reloaded?Hasan
@IBOutlets have not been set yet in didMoveToSuperview().Physical
B
14

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

Breslau answered 31/1, 2018 at 19:49 Comment(1)
@IBOutlets have not been set yet in didMoveToSuperview().Physical
S
8

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!

Schumann answered 4/6, 2014 at 6:54 Comment(0)
W
6

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.
  }
}
Whatever answered 10/3, 2016 at 17:54 Comment(2)
This have nothing to do with Swift 2. It's override point for constraint based layout. Citing the comment in framwork: "override point. called by layoutIfNeeded automatically. As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing."Stabilizer
Warning: layoutSubviews can fire multiple times and is certainly not the place to do any setup code (such as viewDidLoad)Procrustes
T
2

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");

}
Tincher answered 21/2, 2013 at 18:23 Comment(0)
I
0

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.

Idiocrasy answered 16/4, 2012 at 1:36 Comment(2)
I've never seen viewDidLoad not called. Under what circumstances would it not be called?Protector
@devios1, I'd assume if you inserted the subview programmatically after viewDidLoad.Varner
M
-6

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;
Mieshamiett answered 16/4, 2012 at 1:28 Comment(1)
This answer doesn't absolutely reflect the point of question... the asker wants no view controller.Hellenist

© 2022 - 2024 — McMap. All rights reserved.