自动为UITableViewController加载XIB [英] Automatically Loading XIB for UITableViewController

查看:118
本文介绍了自动为UITableViewController加载XIB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进入有趣的事情,想知道我做错了什么或者这是否是正确的行为。

Ran into something interesting, want to know if I'm doing something wrong or if this is the correct behavior.

我有一个自定义的UITableViewController。我假设(第一个错误),如果你这样初始化:

I have a custom UITableViewController. I ASSUMED (first mistake) that if you initialize as such:

[[CustomTableController alloc] init];

它会自动从同名的XIB(CustomTableController.xib)加载,如果它在相同的目录等。

it would automatically load from a XIB of the same name, CustomTableController.xib, if it is in the same directory and such.

HOWEVER

这不起作用;不加载XIB。但是,如果我将我的控制器的父类从'UITableViewController'更改为'UIViewController',那么每个工作都很精细!

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!

呼叫:

[[CustomTableController alloc] init];

从我的xib加载控制器和视图。

loads the controller and view from my xib.

我做错了吗?这是一个错误吗?预期的行为?

Am I doing something wrong? Is this a bug? Expected behavior?

推荐答案

Cocoa Touch中的大多数类列出了一个指定的初始化程序,你应该从你的子类化时的 init 方法。当您创建自己的自定义类时,最好检查文档以查找超类的指定初始化程序。当您使用来自更通用超类的其他初始化程序初始化类时(在这种情况下,您通过调用 - [NSObject init] 进行),您将抢夺直接超类它有机会正确地初始化其状态。有时你可以逃脱这一点。通常你不能。

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.

UIViewController的文档说明它的指定初始化程序是 - initWithNibName:bundle:。如果使用nil nibName调用此方法,它将查找与您的类名匹配的nib。对于 UIViewController -init 的行为没有记录。基于您所看到的行为,似乎它可能正在调用 [self initWithNibName:nil bundle:nil] ,但调用<$ c $会更安全c> initWithNibName:bundle:直接而不是依赖于这种未记录的行为。

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.

UITableViewController只定义一个初始化器, -initWithStyle:(尽管它没有将此方法指定为指定的初始化程序)。这个方法初始化你的UITableViewController而不使用nib,这通常很好。由于您没有将子视图添加到UITableView,因此通过笔尖配置UITableViewController通常没什么好处。

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通过一个nib,文档告诉我们,我们可以安全地绕过 -initWithStyle:并调用UIViewController的 initWithNibName:bundle:方法。以下是文档告诉我们如何在每种情况下初始化我们的UITableView及其控制器:

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:



  • 如果通过 initWithNibName:bundle:方法(由超类UIViewController声明)指定了nib文件,UITableViewController将加载在nib文件中存档的表视图。否则,它会创建一个具有正确尺寸和自动调整大小掩码的未配置UITableView对象。您可以通过tableView属性访问此视图。

  • 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.

如果加载了包含表视图的nib文件,则数据源和委托将成为在nib文件(如果有的话)。如果没有指定nib文件或者nib文件没有定义数据源或委托,UITableViewController会将数据源和表视图的委托设置为self。

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.

总之,大多数Cocoa Touch类的文档要么指定一个指定的初始化程序,要么指定一些可以从子类中安全调用的初始化程序。总是参考超类的文档来确定你的子类应该调用哪个初始化器。

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.

这篇关于自动为UITableViewController加载XIB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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