我什么时候需要调用 -[UIViewController initWithNibName:bundle:]?

发布于 2024-11-09 15:03:39 字数 861 浏览 0 评论 0原文

在帖子 使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2024-11-16 15:03:39

您可以将任何字符串名称传递给 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 。实现应该类似于以下两个示例之一:

- (id)init
{
    self = [super init];
    if (self) {
        // custom initialization
    }
    return self;
}

- (id)init
{
    NSString *aNibName = @"WhateverYouWant";
    NSBundle *aBundle = [NSBundle mainBundle]; // or whatever bundle you want
    self = [self initWithNibName:aNibName bundle:aBundle];
    if (self) {
        // custom initialization
    }
    return self;
}

You can pass any string name to initWithNibName:. You are not just restricted to calling initWithNibName:@"MyClassName" when your class is called MyClassName. It could be initWithNibName:@"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], then initWithNibName: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:

- (id)init
{
    self = [super init];
    if (self) {
        // custom initialization
    }
    return self;
}

or

- (id)init
{
    NSString *aNibName = @"WhateverYouWant";
    NSBundle *aBundle = [NSBundle mainBundle]; // or whatever bundle you want
    self = [self initWithNibName:aNibName bundle:aBundle];
    if (self) {
        // custom initialization
    }
    return self;
}
皓月长歌 2024-11-16 15:03:39
If you want to work following code:

MyViewController *vctrlr = [[MyViewController alloc] inil];
[self.navigationController pushViewController:vctrlr animated:YES];

Then you should implement following both methods in MyViewController:

- (id)init
{
   self = [super initWithNibName:@"MyViewController" bundle:nil];
   if (self != nil)
   {
       // Do initialization if needed
   }
   return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
    NSAssert(NO, @"Init with nib");
    return nil;
}
If you want to work following code:

MyViewController *vctrlr = [[MyViewController alloc] inil];
[self.navigationController pushViewController:vctrlr animated:YES];

Then you should implement following both methods in MyViewController:

- (id)init
{
   self = [super initWithNibName:@"MyViewController" bundle:nil];
   if (self != nil)
   {
       // Do initialization if needed
   }
   return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
    NSAssert(NO, @"Init with nib");
    return nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文