简体   繁体   English

用于UIView的viewDidLoad?

[英]viewDidLoad for UIView?

What is the viewDidLoad for UIView ? UIViewviewDidLoad是什么?

I have a UIView with xib. 我有一个带有xib的UIView I would like to hide one of it's subviews when it is loaded. 我想在加载时隐藏其中一个子视图。 I tried to use this. 我试着用这个。

- (id)initWithCoder:(NSCoder *)aDecoder{
    ....
    _theView.hidden = YES;
}

But the subview _theView is nil at this point. 但是子视图_theView在这一点上是零。

This answer didn't help me, becouse at moment of creating the UIViewController , the UIView is not created yet. 这个答案对我没有帮助,因为在创建UIViewControllerUIView还没有创建。 It is created programaticly, later on. 它是以编程方式创建的,稍后会创建。

Try 尝试

-awakeFromNib method -awakeFromNib方法

Or in xib set the view property hidden for your subview 或者在xib中设置为子视图隐藏的视图属性

AwakeFromNib is called only if the view loaded from nib file. 仅当从nib文件加载视图时才调用AwakeFromNib layoutSubviews is called for all views, you can add bool _loaded = yes; 为所有视图调用layoutSubviews ,可以添加bool _loaded = yes; in the layoutSubviews function and know if the view loaded. layoutSubviews函数中,知道是否加载了视图。

The accepted answer is misleading. 接受的答案是误导性的。 awakeFromNib will always be called, not just if a nib is used. 将始终调用awakeFromNib,而不仅仅是使用nib。 From the apple docs: 来自苹果文档:

awakeFromNib: awakeFromNib:

Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file. 从Interface Builder存档或nib文件加载后,准备接收器以进行维修。

Link 链接

In the next example I've used only a storyBoard You can test this very easily. 在下一个例子中我只使用了一个storyBoard你可以很容易地测试它。

This is our ViewController: 这是我们的ViewController:

在此输入图像描述

ViewController.m: ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"viewDidLoad");
}

-(void)awakeFromNib
{
    NSLog(@"awakeFromNib in view controller");
}

@end

RedView.m: RedView.m:

#import "RedView.h"

@implementation RedView

-(void)awakeFromNib
{
        NSLog(@"awakeFromNib inside RedView");
        self.green.hidden = YES;
}

@end

Order of print: 印刷顺序:

  1. awakeFromNib in view controller 视图控制器中的awakeFromNib
  2. awakeFromNib inside RedView RedView中的awakeFromNib
  3. viewDidLoad viewDidLoad中

And of course the green view will be hidden. 当然,绿色视图将被隐藏。


Edit: 编辑:

awakeFromNib won't be called if you use only code to create your view but you can call it yourself or better yet, create your own method. 如果您仅使用代码创建视图但是您可以自己或更好地调用它,则不会调用awakeFromNib,创建自己的方法。

Example without a StoryBoard (only code): 没有StoryBoard的示例(仅代码):

RedView.m: RedView.m:

#import "RedView.h"

@implementation RedView

-(void)loadRedView
{
    NSLog(@"loadRedView");
    self.green = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    self.green.backgroundColor = [UIColor greenColor];
    [self addSubview:self.green];
    self.green.hidden = YES;
}
@end

ViewController.m: ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.red = [[RedView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    self.red.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.red];
    [self.red loadRedView];
}

@end

There is no such method in general. 通常没有这样的方法。 The question is, where is your _theView coming from. 问题是,你的_theView来自哪里。

If your view, including its subview, is loaded from the same nib/xib/storyboard then you can use awakeFromNib which will be called after the complete object hierarchy has been loaded from the archive, so your _theView should be set as well. 如果您的视图(包括其子视图)是从同一个nib / xib / storyboard加载的,那么您可以使用awakeFromNib ,它将在从归档中加载完整的对象层次结构后调用,因此您的_theView应该被设置。

If your view is created programmatically but does not create the subview for _theView itself, that means there has to be a place in your code where you add that subview. 如果您的视图是以编程方式创建的,但不为_theView本身创建子视图,则意味着您的代码中必须有一个位置,您可以在其中添加该子视图。 In that case you have two options 在这种情况下,您有两个选择

  • Either hide _theView from the caller after you added it 添加后,可以从调用方隐藏_theView
  • Or declare a prepareForDisplay method (or similar) on your view class and call that after your view has been created and _theView has been assigned. 或者在视图类上声明prepareForDisplay方法(或类似方法),并在创建视图并分配_theView后调用它。 In that prepareForDisplay (or whatever name you choose) method you can do whatever you like, eg hide _theView . prepareForDisplay (或您选择的任何名称)方法中,您可以执行任何您喜欢的操作,例如隐藏_theView

I would not recommend to abuse layoutSubviews for this as it is meant for a different purpose and will be called several times during the lifetime of a view, not just once as you want it to be. 我不建议滥用layoutSubviews ,因为它用于不同的目的,并且在视图的生命周期中将被调用多次,而不是像你想要的那样被调用一次。 Yes you can save whether it was called before, but I would consider that a hack as well. 是的你可以保存它之前是否被调用,但我也认为这也是一个黑客攻击。 Better create your own method to initialize the view in a way you want after you set it up correctly and call that. 更好地创建自己的方法,以便在正确设置并调用之后以所需的方式初始化视图。

layoutSubviews will be call for all the views you can set you view as hidden there instead of awakeFromNib. layoutSubviews将调用您可以设置为隐藏的所有视图而不是awakeFromNib。 If you are using xib then you can set the default hidden property. 如果您使用的是xib,则可以设置默认的隐藏属性。

private var layoutSubviewsCounter = 0
override func layoutSubviews() {
    super.layoutSubviews()
    if layoutSubviewsCounter == 0 {
        layoutSubviewsCounter += 1
        viewDidLoad()
    }
}

func viewDidLoad() {
    // your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM