iPhone - 无法访问 IBOutlet(零值)
我在访问 IBOutlet 时遇到问题。
我有一个 NIB,里面有一个表格视图、一个工具栏和一个 UILabel(封装到视图中)。 定义为:
@interface ChoixPeriodeController : UIViewController <UITableViewDelegate> {
IBOutlet UILabel* __periodeInitialeLabel;
}
@property(nonatomic, retain) UILabel* periodeInitialeLabel;
- (void) setSelectedPeriode:(Tache_TypePeriode)typePeriode;
控制器(即文件的所有者)
在 .m 文件中,父窗口调用此函数来初始化 Label :
- (void) setSelectedPeriode:(Tache_TypePeriode)typePeriode {
NSMutableString* tmpString = [NSMutableString string];
[tmpString appendFormat:some text format....];
self.periodeInitialeLabel.text = tmpString;
}
在该函数中,我可以看到 self.periodeInitialeLabel 为零。我不明白为什么。一切都连接到 IB...您知道可能出现什么问题吗?
I have a problem accessing a IBOutlet.
I have a NIB inside which there is a tableview, a toolbar and an UILabel (encapsulated into a view).
The controller (that is the file's owner) is defined as:
@interface ChoixPeriodeController : UIViewController <UITableViewDelegate> {
IBOutlet UILabel* __periodeInitialeLabel;
}
@property(nonatomic, retain) UILabel* periodeInitialeLabel;
- (void) setSelectedPeriode:(Tache_TypePeriode)typePeriode;
with @synthetize periodeInitialeLabel = __periodeInitialeLabel;
In the .m file, this function is called by the parent window to init the Label :
- (void) setSelectedPeriode:(Tache_TypePeriode)typePeriode {
NSMutableString* tmpString = [NSMutableString string];
[tmpString appendFormat:some text format....];
self.periodeInitialeLabel.text = tmpString;
}
Into this function, I can see that self.periodeInitialeLabel is at nil. I can't see why. Everything is connected into IB... Do you see what can be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的问题是操作顺序。如果文本标签仍然为零,则可能尚未设置。相反,您可以在 UIViewController 方法
viewDidLoad
内部调用此方法来处理此类必须执行 Interface Builder 中无法完成的“额外配置”的操作。还有
awakeFromNib
。它们非常相似,只不过 awakeFromNib 在 NIB 文件解包时被调用 ONCE 。viewDidLoad
可以被调用多次 - 例如,如果出现内存不足的情况(通常发生在 iOS4 上),您的视图将通过viewDidUnload
被清除。The question here is the order of operations. If the text label is still nil, it's likely it hasn't been set up yet. Instead, you can call this method inside of the UIViewController method
viewDidLoad
to handle operations like this that must do "extra configuration" that couldn't be done in Interface Builder.There is also
awakeFromNib
. They are very similar, except that awakeFromNib is called ONCE when the NIB file is unpackaged.viewDidLoad
can be called many times - for example, if there is a low memory situation (often happens on iOS4), your views will be purged viaviewDidUnload
.对于仍然遇到此问题的人,即使在示例应用程序中,我也面临着类似的问题。答案是这样的:
UIImageView 在界面中创建Builder Set To nil When Needed
加载属性,同时:
仅访问当前值,该值将保持为零。所以我试图避免使用“.”。操作员。这是我最近在很多示例代码中看到的一个非常微妙的问题。较新示例中的下划线方法:
仍然将成员变量保留为 nil,直到您使用
[self memberVar]
访问它。我认为苹果可能试图通过在实现中引用 _memberVar 来避免这个问题(例如在dealloc
和[_imageView release]
中)更新: 此外,如果您使用
initWithNibName
或loadNibNamed
从 nib 或 xib 动态加载控制器(例如自定义UITableViewCell
) ,不要忘记调用:这也会加载成员变量。除非显示视图或将
pushViewController
推送到navigationController
上,否则它们不会自动加载。For anyone still having trouble with this, I was facing something similar even in a sample app. The answer turned out to be this:
UIImageView Created In Interface Builder Set To nil When Needed
Loads the property, while:
Just accesses the current value, which will remain nil. So I'm trying to get away from using the "." operator. This is a very subtle issue that I've seen in a lot of sample code lately. The underscore method in the newer examples:
still just leaves the member variable nil until you access it with
[self memberVar]
. I think Apple may be trying to avoid the issue by referring to _memberVar in the implementation ( for example indealloc
with[_imageView release]
)UPDATE: Also, if you are using
initWithNibName
orloadNibNamed
to load a controller from a nib or xib on the fly (for example a customUITableViewCell
), don't forget to call:This also loads the member variables. They aren't loaded automatically unless the view is shown or does
pushViewController
onto anavigationController
.我认为您的属性也必须使用 IBOutlet 定义,例如:
并且您需要
@synthesize
实现文件中的属性,然后确保periodeInitialeLabel
已连接在 IB 中正确(即不是“__periodeInitialeLabel”);更新:
如果您的综合语句如下所示:
那么这可能是您的问题。我认为它必须是这样的:
将它与你的领域联系起来。
I think your property has to be defined with IBOutlet as well, like:
and you need to
@synthesize
the property in your implementation file, and then make sureperiodeInitialeLabel
is wired up correctly in IB (i.e. not `__periodeInitialeLabel');Update:
If your synthesize statement looks like this:
then that's probably your problem. I think it has to be this:
to hook it up to your field.
给定这个接口:
它的实现应该如下所示:
我还建议您查看 Google Objective-C 风格指南。用两个前导下划线命名 ivars 可能不是最佳选择。
Given this interface:
its implementation should look like this:
I would also suggest you to look at the Google Objective-C Style Guide. Naming your ivars with two leading underscores may not be the best choice.