我是 iPhone 开发的新手,正在练习一点。正如许多教程所说,我将通过 IB 制作的 uilabel 连接到我的代码中的 IBOutlet 但是在尝试设置它的文本时它仍然说它是空的?我在我的 .h 类上定义了 IBOutlet 对象,并通过 IB 很好地连接它,没有问题,但我知道为什么它仍然为空。任何帮助将不胜感激。
问问题
1303 次
2 回答
0
好的,首先,让我们删除一些无关紧要的东西,并专注于您需要的核心。
#import "CalculatorBrain.h"
@interface CalculatorViewController : UIViewController
{
CalculatorBrain* _calculatorModel;
UILabel *display;
}
- (IBAction) digitPressed:(UIButton *)sender;
@property (nonatomic, retain) IBOutlet UILabel *display;
@end
#import "CalculatorViewController.h"
@implementation CalculatorViewController
@synthesize display;
- (void)dealloc
{
[display release], display = nil;
[_calculatorModel release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (! _calculatorModel)
{
_calculatorModel = [[CalculatorBrain alloc] init];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"display is: %@", display);
}
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *currentDigit = [[sender titleLabel] text];
[display setText:[NSString stringWithFormat:@"%@", currentDigit]];
}
@end
让我们知道当您在 InterfaceBuilder 中设置标签(显示)和操作(digitPressed:)时会发生什么。
于 2010-12-14T23:36:43.243 回答
-2
尝试将您的属性更改为复制(或保留,但在这种情况下,复制更为惯用):
@property (copy) IBOutlet UILabel *display;
您的assign
属性不会增加字符串的引用计数,因此无法保证它在您需要时仍然存在。
于 2010-12-14T23:32:13.723 回答