ios 正确使用 loadView 和 viewDidLoad 与 UIViewController 没有 nibs/xibs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8876212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper use of loadView and viewDidLoad with UIViewController without nibs/xibs
提问by Draco
When I program without a nib, I am under the impression that I need to call loadView to initialize my view, like this:
当我在没有笔尖的情况下编程时,我的印象是我需要调用 loadView 来初始化我的视图,如下所示:
- (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.)
(我已经设置了 nibNameOrNil = nil,因为没有 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.
这是在 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.
我仍然不确定是否应该以这种方式调用 loadView 和 viewDidLoad。他们不会被自动调用。
What is confusing is in the documentation for the UIViewController class reference:
令人困惑的是 UIViewController 类参考的文档:
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.
加载查看讨论
你永远不应该直接调用这个方法。当请求视图属性但当前为 nil 时,视图控制器调用此方法。 如果手动创建视图,则必须覆盖此方法并使用它来创建视图。如果你使用 Interface Builder 创建你的视图并初始化视图控制器——也就是说,你使用 initWithNibName:bundle: 方法初始化视图,直接设置 nibName 和 nibBundle 属性,或者在 Interface Builder 中创建你的视图和视图控制器——那么你不能覆盖这个方法。
So, I don't understand how loadView gets called if I should never call it directly.
所以,如果我不应该直接调用 loadView,我不明白它是如何被调用的。
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.
此方法的默认实现查找有效的 nib 信息并使用该信息加载关联的 nib 文件。 如果没有指定笔尖信息,默认实现会创建一个普通的 UIView 对象并使其成为主视图。
I don't understand how that works -- the creation of a pain UIView.
我不明白这是如何工作的——创建一个痛苦的 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.
如果为了手动创建视图而重写此方法,则应该这样做并将层次结构的根视图分配给视图属性。(您创建的视图应该是唯一的实例,不应与任何其他视图控制器对象共享。)此方法的自定义实现不应调用 super。
如果要对视图执行任何其他初始化,请在 viewDidLoad 方法中执行此操作。在 iOS 3.0 及更高版本中,您还应该覆盖 viewDidUnload 方法以释放对视图或其内容的任何引用。
Okay, so far it doesn't say how viewDidLoad is called. So for viewDidLoad:
好的,到目前为止还没有说明 viewDidLoad 是如何调用的。所以对于 viewDidLoad:
viewDidLoad Discussion
This method is calledafter the view controller has loaded its associated views into memory. This method is called regardlessof 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.
viewDidLoad 讨论
在视图控制器将其关联的视图加载到内存后调用此方法。无论视图是存储在 nib 文件中还是在 loadView 方法中以编程方式创建,都会调用此方法。此方法最常用于对从 nib 文件加载的视图执行额外的初始化步骤。
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.
由于这些方法不会在我的代码中自动调用,因此我认为我必须自己调用它们。但是我仍然没有从文档中清楚地理解这是正确的做法。
回答by Firoze Lafeer
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.
这意味着如何工作是 UIKit 的代码将在实际需要在屏幕上呈现该控制器的视图层次结构时调用您的 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.
具体来说,如果任何代码尝试读取您view
的视图控制器上的属性,并且该属性为零,那么 UIViewController 的代码将调用该loadView
方法。如果您不loadView
自己实现,则默认实现将退回到尝试从笔尖加载视图层次结构。
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.
如果这些方法没有在你的视图控制器中被调用,那么你呈现这个视图控制器的方式肯定有问题,这会阻止 UIViewController 中的框架代码调用这些方法。发布该代码,有人可以帮助您找出该错误。
Before you attempt to fix thatbug, 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]
然后在您自己的 实现中viewDidLoad
,您将需要调用超类的实现[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.
请记住,loadView
您唯一的责任是将 self.view 设置为某个有效的 view。就是这样。UIKit 代码会看到并呼叫viewDidLoad
你。
Hope that helps.
希望有帮助。
回答by Tricertops
The answer by Firoze Lafeeris correct, I just want to show how to force loadthe view property the correct way:
Firoze Lafeer的回答是正确的,我只想展示如何以正确的方式强制加载视图属性:
(void)self.view;
You should not do this!If you must, you are doing something wrong, but do not call -loadView
and -viewDidLoad
yourself at any circumstances.
你不应该这样做!如果你一定要,你正在做的事情不对,但不要打电话-loadView
,并-viewDidLoad
在任何情况下自己。