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" bundle:[NSBundle mainBundle]]autorelease];
之间有什么区别
我尝试用[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner: theTestController options:nil];
替换theTestController = [[[CustomCell alloc]initWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]]autorelease];
,但我遇到了错误。如果我使用后者,看起来商店仍然是nil
。
答案 0 :(得分:6)
initWithNibName:bundle:
是在UIViewController
中声明的便捷方法,可用于其子类。这将通过加载nib来初始化视图控制器,可能是在内部使用loadNibName:owner:options:
方法。
initWithNibName:bundle:
及其子类无法使用 UIView
。因此,我们必须使用loadNibName:owner:options:
来加载视图。
UIView
子类,因此可以使用loadNibName:owner:options:
。initWithNibName:bundle:
是UIViewController
初始化的便捷方法。