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

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

问题描述

当我编程没有nib,我的印象是,我需要调用loadView初始化我的视图,像这样:

   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
self = [super initWithNibName:nil bundle:nil];
if(self){
//自定义初始化
[self loadView];
}
return self;
}

(我设置了nibNameOrNil = nil,因为没有nib。 / p>

然后,我设置视图,像这样:

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

这是做viewDidLoad中的一切。



我仍然不确定我是否应该这样调用loadView和viewDidLoad。它们不会自动调用。



令人困惑的是在UIViewController类引用的文档中:


loadView讨论



您不应直接调用此方法。视图控制器调用
此方法请求视图属性,但目前为零。
如果您手动创建视图,则必须覆盖此方法,并且
使用它来创建视图。
如果使用Interface Builder创建
视图并初始化视图控制器 - 即,使用initWithNibName:bundle:方法初始化
视图,直接设置nibName和
nibBundle属性,或者在Interface Builder中创建视图和视图
controller -


所以,我不明白loadView如何调用,如果我应该从来不直接叫它。


此方法的默认实现查找有效的nib
信息,并使用该信息加载关联的nib文件。
如果没有指定nib信息,默认实现创建
a纯UIView对象,并将其作为主视图。


我不明白如何工作 - 创建一个痛苦的UIView。


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



如果要对视图执行任何额外的初始化,请在viewDidLoad方法中执行
。在iOS 3.0及更高版本中,您还应该
重写viewDidUnload方法以释放对
视图或其内容的任何引用。


好吧,到目前为止它不说怎么调用viewDidLoad。所以对于viewDidLoad:


viewDidLoad讨论



之后,视图控制器将其
关联视图加载到内存中。 无论
调用此方法,无论视图是存储在nib文件中还是在loadView方法中以编程方式创建
。这种方法最常见的是
用于对从nib文件加载的
视图执行附加的初始化步骤。


由什么调用?



由于这些方法没有在我的代码中自动调用,我只能认为我必须自己调用它们。但我仍然不能从文档中清楚地了解这是正确的操作。

解决方案



这是如何工作的,UIKit的代码会调用你的loadView方法if和when



具体来说,如果任何代码试图读取您的视图属性,并且该属性为nil,那么UIViewController的代码将调用 loadView 方法。如果你自己不实现 loadView ,那么默认实现将回退到尝试从nib加载视图层次结构。



当您尝试呈现视图控制器并且视图为零时,这一切都会自动发生。 (如果你的视图控制器必须卸载它的视图响应内存压力,另一个行为,你'免费',并且你不想打电话给自己),这可以发生多次,而你的应用程序运行)



如果这些方法没有在你的视图控制器中被调用,那么你如何呈现这个视图控制器是有问题的,这阻止了UIViewController中的框架代码调用这些方法。



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

  [self loadView] 
[self viewDidLoad]

然后在您自己的 viewDidLoad 实现中,您将要调用超类的实现 [super viewDidLoad]



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



希望有帮助。


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;
}

(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];
}

This is do everything else in 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.

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

loadView Discussion

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.

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

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.

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

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.

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.

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

viewDidLoad Discussion

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.

Called by what?

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.

解决方案

As the documentation says, you should not call these methods yourself.

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.

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)

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]

And then in your own implementation of viewDidLoad, you will want to call the superclass' implementation with [super 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.

Hope that helps.

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

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