我什么时候需要调用 -[UIViewController initWithNibName:bundle:]?
在帖子 使用 initWithNibName 绝对不会改变中,他展示了同一 View Nib 定义的两种用法,在第一种情况下,他只需调用 alloc/init ,而在第二种情况下,他指定 initWithNibName 。
所以,虽然这总是有效:
MyViewController *vctrlr = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]; [self.navigationController PushViewController:vctrlr 动画:是]; [vctrlr 发布];
以下内容适用于我继承的所有视图控制器,但不适用于我的!
<代码> TheirViewController *vctrlr = [[TheirViewController alloc] init]; [self.navigationController PushViewController:vctrlr 动画:是]; [vctrlr 发布];
iOS 编程新手,我继承了一些代码。所有视图控制器的视图均在 IB 中定义,但这些视图控制器的分配/初始化创建不一致。我创建了一个新的视图控制器和 XIB,但除非我使用 initWithNibName,否则它不起作用(当我将视图控制器推到导航控制器上时,它会崩溃)。我不知道我的视图控制器与其他视图控制器有何不同......有什么提示吗?我能够删除应用程序中除我的之外的所有其他视图控制器的 initNibName 用法。
In post Using initWithNibName changes absolutely nothing, he shows two uses of the same View Nib definition, in the first case, he simply calls alloc/init and the second, he specifies initWithNibName.
So, while this always works:
MyViewController *vctrlr = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self.navigationController pushViewController:vctrlr animated:YES];
[vctrlr release];
The following works for all the View Controllers I've inherited, but not mine!
TheirViewController *vctrlr = [[TheirViewController alloc] init];
[self.navigationController pushViewController:vctrlr animated:YES];
[vctrlr release];
New to iOS programming, I inherited some code. All the View Controllers' views are defined in IB, but there was inconsistent allocation/init creation of those view controllers. I created a new View Controller and XIB, but it does not work unless I use initWithNibName (it crashes when I push the view controller onto the Nav Controller). I cannot tell how my view controller is different than the others... any hints? I was able to delete the initNibName usage for all the other view controllers in the app except mine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将任何字符串名称传递给
initWithNibName:
。当您的类被称为MyClassName
时,您不仅限于调用initWithNibName:@"MyClassName"
。它可以是initWithNibName:@"MyClassNameAlternateLayout"
。如果您需要根据应用程序需要执行的操作加载不同的笔尖,这将变得非常有用。虽然我尝试尽可能为每个设备类别(iPhone 或 iPad)每个视图控制器一个笔尖,以使开发和维护更简单,但我可以理解开发人员有时是否希望提供不同的布局或不同的功能。
另一个重要的一点是 initWithNibName:bundle: 是 UIViewController 的指定初始值设定项。当您调用
-[[UIViewController alloc] init]
时,会在后台调用initWithNibName:bundle:
。您可以使用符号断点来验证这一点。换句话说,如果您只想要默认行为,则可以调用-[[UIViewController alloc] init]
并且将隐式调用指定的初始化程序。但是,如果您调用
-[[UIViewController alloc] init]
并没有获得预期的行为,则可能是您的 UIViewController 子类错误地实现了- (id)init
。实现应该类似于以下两个示例之一:或
You can pass any string name to
initWithNibName:
. You are not just restricted to callinginitWithNibName:@"MyClassName"
when your class is calledMyClassName
. It could beinitWithNibName:@"MyClassNameAlternateLayout"
.This becomes useful if you need to load a different nib depending on what the app needs to do. While I try to have one nib per view controller per device category (iPhone or iPad) whenever possible to make development and maintenance simpler, I could understand if a developer would want to provide a different layout or different functionality at times.
Another important point is that initWithNibName:bundle: is the designated initializer for
UIViewController
. When you call-[[UIViewController alloc] init]
, theninitWithNibName:bundle:
is called behind the scenes. You can verify this with a symbolic breakpoint. In other words, if you simply want the default behavior, it is expected that you can call-[[UIViewController alloc] init]
and the designated initializer will be called implicitly.If, however, you are calling
-[[UIViewController alloc] init]
and not getting the expected behavior, it's likely that your UIViewController subclass has implemented- (id)init
incorrectly. The implementation should look like one of these two examples:or