theTestController = [[[CustomCell alloc]initWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]]autorelease];
我的猜测是,它将加载 CustomCell 并将 theTestController 设置为所有者。如果是这样:
为什么在
cellForRowAtIndexPath
的大多数示例代码中我看到[[NSBundle mainBundle] loadNibNamed:@"CustomCell"owner: theTestController options:nil]
;而是?[[NSBundle mainBundle] loadNibNamed:@"CustomCell"owner: theTestController options:nil];
和theTestController = [[[CustomCell alloc]initWithNibName] 有什么区别:@"CustomCell"包:[NSBundle mainBundle]]autorelease];
我尝试用
theTestController = [[[CustomCell alloc]initWithNibName:@ “CustomCell”bundle:[NSBundle mainBundle]]autorelease];
我收到错误。如果我使用后者,看起来 socket 仍然是nil
。
最佳答案
initWithNibName:bundle:
是在 UIViewController
中声明的便捷方法,可供其子类使用。这将通过加载 Nib 来初始化 View Controller ,可能是通过在内部使用 loadNibName:owner:options:
方法。
initWithNibName:bundle:
对于 UIView
及其子类不可用。因此我们必须使用 loadNibName:owner:options:
来加载 View 。
- 自定义单元格是
UIView
子类,因此使用loadNibName:owner:options:
。 - 差别不大。
initWithNibName:bundle:
是UIViewController
初始化的便捷方法。 - 由于上述原因,您收到错误。
关于objective-c - initWithNibName 到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401350/