调用 initWithNibName 不会初始化笔尖中的项目,它有 0x0 [英] calling initWithNibName doesn't initialize items in the nib, it has 0x0

查看:18
本文介绍了调用 initWithNibName 不会初始化笔尖中的项目,它有 0x0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打电话时:

self.viewController = [[DidItViewController alloc] initWithNibName:@"DidItViewController" bundle:nil];

然后我在调试器中执行此行后立即检查self.viewController.navController,我发现它是空的(0x0).

and then I check self.viewController.navController right after this line is executed in the debugger, I find that it's empty (0x0).

在 DidItViewController 上,我将 navController 定义为:

On DidItViewController I have my navController defined as:

IBOutlet NavigationController *navController; 

在我的 nib 文件中,我将 NavigationController 绑定到文件所有者(DidItViewController)上的这个 navController 属性.

and in my nib file I have the NavigationController bound to this navController property on the File Owner (a DidItViewController).

为什么我的 navController 没有被创建?有任何想法吗?我想我可能遗漏了一些关于 initWithNibName 工作方式的信息...

Why doesn't my navController get created? Any ideas? I think I may be missing something about the way initWithNibName works..

谢谢.

推荐答案

或者你可以使用 [NSBundle loadNibNamed:owner:options:] 方法代替.此方法可确保所有插座连接均已连接.(其中 [UIViewController initWithNibName: bundle:] 没有)

Or you can use [NSBundle loadNibNamed:owner:options:] method instead of. This method ensures all outlet connections connected. (which [UIViewController initWithNibName: bundle:] doesn't)

在这种情况下,NIB 中的 File's Owner 是 PhotoShow 类的外部实例.

In this case, File's Owner in the NIB is an external instance of PhotoShow class.

// This works completely. All outlets works.
PhotoShow* obj = [[PhotoShow alloc] init];
[[NSBundle mainBundle] loadNibNamed:@"PhotoShow" owner:obj options:nil];
// Outlets are always available at this moment.

// This works. but does not connects outlets correctly sometimes.
PhooShow* obj = [[PhotoShow alloc] initWithNibName:@"PhotoShow" bundle:[NSBundle mainBundle]];
// Outlets may not available at this moment.

参考文献选择

您可以使用此方法加载用户界面并使对象可用于您的代码.在加载过程中,此方法会取消归档每个对象,对其进行初始化,将其属性设置为其配置值,并重新建立与其他对象的任何连接.(为了建立outlet连接,该方法使用setValue:forKey:方法,可能会导致outlet中的对象被自动保留.)关于nib加载过程的详细信息,请参见资源编程指南.

Selection from reference document

You can use this method to load user interfaces and make the objects available to your code. During the loading process, this method unarchives each object, initializes it, sets its properties to their configured values, and reestablishes any connections to other objects. (To establish outlet connections, this method uses the setValue:forKey: method, which may cause the object in the outlet to be retained automatically.) For detailed information about the nib-loading process, see Resource Programming Guide.

这篇关于调用 initWithNibName 不会初始化笔尖中的项目,它有 0x0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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