iPhone - 无法访问 IBOutlet(零值)

发布于 2024-10-12 01:04:37 字数 745 浏览 10 评论 0原文

我在访问 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 技术交流群。

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

发布评论

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

评论(4

心是晴朗的。 2024-10-19 01:04:37

这里的问题是操作顺序。如果文本标签仍然为零,则可能尚未设置。相反,您可以在 UIViewController 方法 viewDidLoad 内部调用此方法来处理此类必须执行 Interface Builder 中无法完成的“额外配置”的操作。

还有awakeFromNib。它们非常相似,只不过 awakeFromNib 在 NIB 文件解包时被调用 ONCEviewDidLoad 可以被调用多次 - 例如,如果出现内存不足的情况(通常发生在 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 via viewDidUnload.

灵芸 2024-10-19 01:04:37

对于仍然遇到此问题的人,即使在示例应用程序中,我也面临着类似的问题。答案是这样的:

UIImageView 在界面中创建Builder Set To nil When Needed

[self memberVar]

加载属性,同时:

self.memberVar

仅访问当前值,该值将保持为零。所以我试图避免使用“.”。操作员。这是我最近在很多示例代码中看到的一个非常微妙的问题。较新示例中的下划线方法:

@synthesize memberVar = _memberVar;

仍然将成员变量保留为 nil,直到您使用 [self memberVar] 访问它。我认为苹果可能试图通过在实现中引用 _memberVar 来避免这个问题(例如在 dealloc[_imageView release] 中)


更新: 此外,如果您使用 initWithNibNameloadNibNamed 从 nib 或 xib 动态加载控制器(例如自定义 UITableViewCell) ,不要忘记调用:

[self loadView];

这也会加载成员变量。除非显示视图或将 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

[self memberVar]

Loads the property, while:

self.memberVar

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:

@synthesize memberVar = _memberVar;

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 in dealloc with [_imageView release] )


UPDATE: Also, if you are using initWithNibName or loadNibNamed to load a controller from a nib or xib on the fly (for example a custom UITableViewCell), don't forget to call:

[self loadView];

This also loads the member variables. They aren't loaded automatically unless the view is shown or does pushViewController onto a navigationController.

寒江雪… 2024-10-19 01:04:37

我认为您的属性也必须使用 IBOutlet 定义,例如:

@property(nonatomic, retain) IBOutlet UILabel* periodeInitialeLabel; 

并且您需要 @synthesize 实现文件中的属性,然后确保 periodeInitialeLabel 已连接在 IB 中正确(即不是“__periodeInitialeLabel”);

更新:

如果您的综合语句如下所示:

@synthesize periodeInitialeLabel;

那么这可能是您的问题。我认为它必须是这样的:

@synthesize periodeInitialeLabel = __periodeInitialeLabel;

将它与你的领域联系起来。

I think your property has to be defined with IBOutlet as well, like:

@property(nonatomic, retain) IBOutlet UILabel* periodeInitialeLabel; 

and you need to @synthesize the property in your implementation file, and then make sure periodeInitialeLabel is wired up correctly in IB (i.e. not `__periodeInitialeLabel');

Update:

If your synthesize statement looks like this:

@synthesize periodeInitialeLabel;

then that's probably your problem. I think it has to be this:

@synthesize periodeInitialeLabel = __periodeInitialeLabel;

to hook it up to your field.

咽泪装欢 2024-10-19 01:04:37

给定这个接口:

@interface ChoixPeriodeController : UIViewController <UITableViewDelegate> {    
    UILabel* __periodeInitialeLabel;
}

// I think it's more appropriate to mark a property with IBOutlet,
// rather than an ivar
@property(nonatomic, retain) IBOutlet UILabel* periodeInitialeLabel;

- (void) setSelectedPeriode:(Tache_TypePeriode)typePeriode;

// ...

@end

它的实现应该如下所示:

@implementation ChoixPeriodeController

@synthesize
periodeInitialeLabel = __periodeInitialeLabel;

// the rest of your implementation goes here
// ...

@end

我还建议您查看 Google Objective-C 风格指南。用两个前导下划线命名 ivars 可能不是最佳选择。

Given this interface:

@interface ChoixPeriodeController : UIViewController <UITableViewDelegate> {    
    UILabel* __periodeInitialeLabel;
}

// I think it's more appropriate to mark a property with IBOutlet,
// rather than an ivar
@property(nonatomic, retain) IBOutlet UILabel* periodeInitialeLabel;

- (void) setSelectedPeriode:(Tache_TypePeriode)typePeriode;

// ...

@end

its implementation should look like this:

@implementation ChoixPeriodeController

@synthesize
periodeInitialeLabel = __periodeInitialeLabel;

// the rest of your implementation goes here
// ...

@end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文