xcode 为 UITableViewController 自动加载 XIB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2596336/
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
Automatically Loading XIB for UITableViewController
提问by ACBurk
Ran into something interesting, want to know if I'm doing something wrong or if this is the correct behavior.
遇到一些有趣的事情,想知道我是否做错了什么,或者这是否是正确的行为。
I have a custom UITableViewController. I ASSUMED (first mistake) that if you initialize as such:
我有一个自定义的 UITableViewController。我假设(第一个错误)如果你这样初始化:
[[CustomTableController alloc] init];
it would automatically load from a XIB of the same name, CustomTableController.xib, if it is in the same directory and such.
它会自动从同名的 XIB 加载,CustomTableController.xib,如果它在同一个目录中等等。
HOWEVER
然而
This does not work; doesn't load the XIB. BUT, if I change the parent class of my controller from 'UITableViewController' to 'UIViewController', EVERYHTING WORKS FINE!
这不起作用;不加载 XIB。但是,如果我将控制器的父类从“UITableViewController”更改为“UIViewController”,则一切正常!
Calling:
调用:
[[CustomTableController alloc] init];
loads the controller and view from my xib.
从我的 xib 加载控制器和视图。
Am I doing something wrong? Is this a bug? Expected behavior?
难道我做错了什么?这是一个错误吗?预期行为?
回答by cduhn
Most of the classes in Cocoa Touch list a "designated initializer" that you're supposed to call from your init
methods when you subclass them. When you create your own custom class, it's a good idea to check the documentation to find the designated initializer for your superclass. When you initialize the class using some other initializer from a more general superclass (which you're doing by calling - [NSObject init]
in this case), you rob your direct superclass of its opportunity to properly initialize its state. Sometimes you can get away with this. Often you can't.
Cocoa Touch 中的大多数类都列出了一个“指定的初始化程序”,init
当您对它们进行子类化时,您应该从您的方法中调用它。创建自己的自定义类时,最好查看文档以找到超类的指定初始值设定项。当您使用来自更通用的超类的一些其他初始化程序(您通过调用来执行 -[NSObject init]
在这种情况下)来初始化类时,您就剥夺了您的直接超类正确初始化其状态的机会。有时你可以摆脱这个。通常你不能。
UIViewController's documentation states that its designated initializer is -initWithNibName:bundle:
. If you call this method with a nil nibName, it will look for a nib that matches your class name. The behavior of -init
is undocumented for UIViewController
. Based on the behavior you're seeing, it seems like it may be calling [self initWithNibName:nil bundle:nil]
, but it would be safer to call initWithNibName:bundle:
directly rather than relying on this undocumented behavior.
UIViewController 的文档说明其指定的初始值设定项是 - initWithNibName:bundle:
。如果您使用 nil nibName 调用此方法,它将查找与您的类名匹配的 nib。的行为-init
没有记录在案UIViewController
。根据您所看到的行为,它似乎可能正在调用[self initWithNibName:nil bundle:nil]
,但initWithNibName:bundle:
直接调用而不是依赖于这种未记录的行为会更安全。
UITableViewController only defines a single initializer, -initWithStyle:
(although it doesn't specify this method as the designated initializer). This method initializes your UITableViewController without using a nib, which is usually fine. Since you don't add subviews to a UITableView, there usually isn't much to be gained by configuring your UITableViewController via a nib.
UITableViewController 只定义了一个初始化器,-initWithStyle:
(虽然它没有指定这个方法作为指定的初始化器)。此方法在不使用笔尖的情况下初始化您的 UITableViewController,这通常很好。由于您不向 UITableView 添加子视图,因此通过笔尖配置您的 UITableViewController 通常不会获得太多收益。
If decide you want to configure your UITableViewController via a nib anyway, the documentation tells us that we can safely bypass -initWithStyle:
and call UIViewController's initWithNibName:bundle:
method. Here is what the documentation tells us about how our UITableView and its controller will be initialized in each case:
如果决定无论如何要通过笔尖配置 UITableViewController,文档告诉我们我们可以安全地绕过-initWithStyle:
并调用 UIViewController 的initWithNibName:bundle:
方法。以下是文档告诉我们在每种情况下如何初始化 UITableView 及其控制器的内容:
If a nib file is specified via the
initWithNibName:bundle:
method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.
如果通过
initWithNibName:bundle:
方法(由超类 UIViewController 声明)指定了 nib 文件,则 UITableViewController 加载存档在 nib 文件中的表视图。否则,它会创建一个具有正确尺寸和自动调整大小掩码的未配置 UITableView 对象。您可以通过 tableView 属性访问此视图。如果加载了包含表视图的 nib 文件,则数据源和委托将成为 nib 文件(如果有)中定义的那些对象。如果未指定 nib 文件或 nib 文件未定义数据源或委托,则 UITableViewController 将表视图的数据源和委托设置为 self。
In summary, the documentation for most Cocoa Touch classes either specify a single designated initializer or a handful of initializers that you can call safely from your subclasses. Always refer to the documentation for your superclass to figure out which initializer your subclass should call.
总之,大多数 Cocoa Touch 类的文档要么指定了一个指定的初始化程序,要么指定了一些您可以从子类中安全调用的初始化程序。请始终参考您的超类的文档,以确定您的子类应该调用哪个初始化程序。