正确使用 loadView 和 viewDidLoad 与 UIViewController 没有 nibs/xibs [英] Proper use of loadView and viewDidLoad with UIViewController without nibs/xibs

查看:17
本文介绍了正确使用 loadView 和 viewDidLoad 与 UIViewController 没有 nibs/xibs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在没有笔尖的情况下编程时,我的印象是我需要调用 loadView 来初始化我的视图,如下所示:

When I program without a nib, I am under the impression that I need to call loadView to initialize my view, like this:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        // Custom initialization
        [self loadView];
    }
    return self;
}

(我已经设置了 nibNameOrNil = nil,因为没有 nib.)

(I have set nibNameOrNil = nil, since there is not nib.)

然后,我设置了视图,如下所示:

Then, I set up the view, like this:

- (void) loadView {
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
    [self viewDidLoad];
}

这是在 viewDidLoad 中执行其他所有操作.

This is do everything else in viewDidLoad.

我仍然不确定是否应该以这种方式调用 loadView 和 viewDidLoad.他们不会被自动调用.

I'm still unsure if I am supposed to make the calls to loadView and viewDidLoad in this way. They are not getting called automatically.

令人困惑的是 UIViewController 类参考的文档:

What is confusing is in the documentation for the UIViewController class reference:

loadView 讨论

loadView Discussion

你不应该直接调用这个方法.视图控制器调用当请求视图属性但当前为零时使用此方法.如果您手动创建视图,则必须覆盖此方法并使用它来创建您的视图. 如果您使用 Interface Builder 创建你的视图并初始化视图控制器——也就是说,你初始化使用 initWithNibName:bundle: 方法的视图,设置 nibName 和直接 nibBundle 属性,或创建您的视图和视图Interface Builder 中的控制器——那么你不能覆盖它方法.

You should never call this method directly. The view controller calls this method when the view property is requested but is currently nil. If you create your views manually, you must override this method and use it to create your views. If you use Interface Builder to create your views and initialize the view controller—that is, you initialize the view using the initWithNibName:bundle: method, set the nibName and nibBundle properties directly, or create both your views and view controller in Interface Builder—then you must not override this method.

所以,如果我不应该直接调用 loadView,我不明白它是如何被调用的.

So, I don't understand how loadView gets called if I should never call it directly.

这个方法的默认实现寻找有效的笔尖信息并使用该信息加载相关的 nib 文件.如果没有指定nib信息,默认实现创建一个普通的 UIView 对象并使其成为主视图.

The default implementation of this method looks for valid nib information and uses that information to load the associated nib file. If no nib information is specified, the default implementation creates a plain UIView object and makes it the main view.

我不明白这是如何工作的——创建一个痛苦的 UIView.

I don't understand how that works -- the creation of a pain UIView.

如果您重写此方法以手动创建视图,您应该这样做并将您的层次结构的根视图分配给查看属性.(您创建的视图应该是唯一的实例并且不应与任何其他视图控制器对象共享.)您的此方法的自定义实现不应调用 super.

If you override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property. (The views you create should be unique instances and should not be shared with any other view controller object.) Your custom implementation of this method should not call super.

如果你想对你的视图执行任何额外的初始化,请执行所以在 viewDidLoad 方法中.在 iOS 3.0 及更高版本中,您还应该覆盖 viewDidUnload 方法以释放对视图或其内容.

If you want to perform any additional initialization of your views, do so in the viewDidLoad method. In iOS 3.0 and later, you should also override the viewDidUnload method to release any references to the view or its contents.

好的,到目前为止还没有说明 viewDidLoad 是如何调用的.所以对于 viewDidLoad:

Okay, so far it doesn't say how viewDidLoad is called. So for viewDidLoad:

viewDidLoad 讨论

viewDidLoad Discussion

此方法在视图控制器加载其后调用关联的视图进入内存.无论调用此方法视图是存储在 nib 文件中还是创建的以编程方式在 loadView 方法中.这种方法最常用用于在视图上执行额外的初始化步骤从 nib 文件加载.

This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.

什么叫?

由于这些方法不会在我的代码中自动调用,所以我认为我必须自己调用它们.但我仍然没有从文档中清楚地理解这是正确的做法.

Since These methods are not getting called automatically in my code, I am left to think that I have to call them myself. But I still don't get a clear understanding form the documentation that this is the right thing to do.

推荐答案

正如文档所说,你不应该自己调用这些方法.

这意味着 UIKit 的代码将在实际需要在屏幕上呈现该控制器的视图层次结构时调用您的 loadView 方法.

How this is meant to work is that UIKit's code will call your loadView method if and when it actually needs to present that controller's view hierarchy on screen.

具体来说,如果任何代码尝试读取您的视图控制器上的 view 属性,并且该属性为零,那么 UIViewController 的代码将调用 loadView 方法.如果您自己没有实现 loadView,那么默认实现将回退到尝试从笔尖加载视图层次结构.

Specifically, if any code attempts to read your view property on your view controller, and that property is nil, then UIViewController's code will call the loadView method. If you don't implement loadView yourself, then the default implementation will fall back to attempting to load the view hierarchy from the nib.

当您尝试呈现视图控制器并且视图为零时,这一切都会自动发生.(如果您的视图控制器必须卸载其视图以响应内存压力,这可能会在您的应用运行时多次发生,这是您免费"获得的另一种行为,并且您不想调用自己)

This all happens automatically when you attempt to present your view controller and the view is nil. (This can happen multiple times while your app is running if your view controller has to unload its view in response to memory pressure, another behavior you get for 'free' and that you don't want to call yourself)

如果这些方法没有在你的视图控制器中被调用,那么你呈现这个视图控制器的方式肯定有问题,这阻止了 UIViewController 中的框架代码调用这些方法.发布该代码,有人可以帮助您找出该错误.

If these methods are not being called in your view controller, then there must be something wrong with how you are presenting this view controller, which is preventing the framework code in UIViewController from calling these methods. Post that code and someone can help you figure out that bug.

在尝试修复那个错误之前,您应该从代码中删除这些:

Before you attempt to fix that bug, though, you should remove these from your code:

[self loadView]
[self viewDidLoad]

然后在您自己的 viewDidLoad 实现中,您将需要使用 [super viewDidLoad]

And then in your own implementation of viewDidLoad, you will want to call the superclass' implementation with [super viewDidLoad]

请记住,在 loadView 中,您唯一的责任是将 self.view 设置为某个有效视图.而已.然后 UIKit 代码会看到并为您调用 viewDidLoad.

Remember that in loadView your only responsibility to set self.view to some valid view. That's it. UIKit code will then see that and call viewDidLoad for you.

希望有所帮助.

这篇关于正确使用 loadView 和 viewDidLoad 与 UIViewController 没有 nibs/xibs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆