calling initWithNibName doesn#39;t initialize items in the nib, it has 0x0(调用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调用 initWithNibName 不会初始化笔尖中的项目,它有 0x0
基础教程推荐
- Android - 如何在runOnUiThread 中将数据传递给Runnable? 2022-01-01
- EditText 中的 setHintTextColor() 2022-01-01
- 更改 UITableView 部分标题的颜色 2022-01-01
- 在视图控制器的宽度上水平均匀分布 UIButton 的最简单方法? 2022-01-01
- UINavigationBar 隐藏按钮文本 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 从 UIWebView 访问元数据 2022-01-01
- Firebase 云消息传递令牌未生成 2022-01-01
- 在 iOS 7 下 CCMenu 错位 2022-01-01
- UINavigationItem 的持久 rightBarButtonItem 属性 2022-01-01