iOS viewDidLoad for UIView

2020-02-16 23:45发布

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.

7条回答
叼着烟拽天下
2楼-- · 2020-02-17 00:05

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.

查看更多
太酷不给撩
3楼-- · 2020-02-17 00:10

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

}
查看更多
你好瞎i
4楼-- · 2020-02-17 00:13

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;
查看更多
Summer. ? 凉城
5楼-- · 2020-02-17 00:16

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

查看更多
欢心
6楼-- · 2020-02-17 00:18

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.

查看更多
贼婆χ
7楼-- · 2020-02-17 00:24

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!

查看更多
登录 后发表回答