0

我已经在一个项目上工作了几个星期,最近实现了一个单例对象来帮助保存数据。实施后,我在主视图控制器中更新标签时遇到问题。

例如,我正在尝试更新以下标签:

@property (nonatomic, retain) IBOutlet UILabel *numDrinksLabel;
@property (nonatomic, retain) IBOutlet UILabel *BACLabel;

使用以下代码,该代码位于按下按钮时调用的函数内:

BACLabel.text = [NSString stringWithFormat:@"%.2f", user.BAC];
numDrinksLabel.text = [NSString stringWithFormat:@"(%i)", user.numDrinks];

这个代码块给了我运行时错误:

-[__NSCFString setText:]: unrecognized selector sent to instance 0x1197ef40

viewDidLoad但是,在内部调用或执行的相同代码块viewDidAppear没有问题。最初这向我表明我的@property声明存在问题,但是当我更改为 时,我得到了同样的错误,当我更改为 时retain,uilabel 对象只是 null,这是意料之中的,但仍然非常令人沮丧。strongweak

所以问题是,为什么标签对象会在viewDidAppear函数之后被释放?

任何有关如何解决此问题或进一步测试根本原因的建议将不胜感激!

4

2 回答 2

0

请检查这些标签的superview是否也声明了strong

于 2013-03-29T19:48:09.480 回答
0

It seems that your object which contains the iVars numDrinksLabel and BACLabel does no longer exist when you assign something to the text property of the UILabel objects.
Since this happens after you press a button, you have been in the main event loop before. In this loop, any autorelease object will be released if it is not retained by some object.
Thus it seems to me that the object that has your UILabels as iVars is an autorelease object, and it is not retained because you don't use setter methods like self.BACLabel.text = but simply assign methods as BACLabel.text =.
So try replacing your assignments like BACLabel.text = by setters like self.BACLabel.text =, as sixthcent said.

于 2013-03-29T20:20:58.513 回答