Ios5 UITextfield和null
假设iPhone项目有以下代码:Ios5 UITextfield和null,ios5,Ios5,假设iPhone项目有以下代码: IBOutlet UITextField* numberDisplay; @property (strong,nonatomic) IBOutlet UITextField *numberDisplay; 在实现文件和 @synthesize numberDisplay; 在实现文件中。同样在实施中的还有 -(IBAction)numberClicked:(id)sender { UIButton *buttonPressed = (UIButto
IBOutlet UITextField* numberDisplay;
@property (strong,nonatomic) IBOutlet UITextField *numberDisplay;
在实现文件和
@synthesize numberDisplay;
在实现文件中。同样在实施中的还有
-(IBAction)numberClicked:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
int val = buttonPressed.tag;
if ( [numberDisplay.text compare:@"0"] == 0 ) {
numberDisplay.text =[NSString stringWithFormat:@"%d", val ];
}
else {
numberDisplay.text = [NSString
stringWithFormat:@"%@%d", numberDisplay.text, val ];
}
}
当我运行应用程序时,UITextfield中没有显示,即使连接是通过IB建立的,并且通过查看inspector证明是建立的。即使作为测试,如果我添加行
numberDisplay.text = @"a";
NSLog(@"the value is %@",numberDisplay.text);
NSLog(@"the value is %@",numberDisplay);
I get“值为(null)在这两种情况下。清理并重建项目,然后再次检查IB是否已连接到相关字段。如果仍然不起作用,您将尝试运行调试器并检查UITextField是否已设置或释放。从ivar中删除IBOutlet,实际上是删除ivar,即奥登运行时将为您合成它 放下ivar,并更改为:
@property (strong,nonatomic) IBOutlet UITextField *numberDisplay;
这是:
@synthesize numberDisplay = _numberDisplay;
此行将自动创建名为_numberDisplay的ivar
-(IBAction)numberClicked:(id)sender {
UIButton *buttonPressed = (UIButton *)sender;
int val = buttonPressed.tag;
if ( [_numberDisplay.text compare:@"0"] == 0 ) {
_numberDisplay.text =[NSString stringWithFormat:@"%d", val ];
}
else {
_numberDisplay.text = [NSString stringWithFormat:@"%@%d", _numberDisplay.text, val ];
}
}
注意:在您自己的类中,您可以使用self.numberDisplay
或\u numberDisplay
引用文本字段
完成后,您很有可能必须在IB中重新连接。请记住断开任何无效的连接。将这些连接放入
视图将显示:
NSLog(@“值ivar为%@”,u numberDisplay);
NSLog(@“值属性为%@”,self.numberDisplay)
对两者的响应都是值为null将那些NSLog
s移动到viewdiload:
的最顶端,并检查结果。没有消息。NSLog似乎没有执行。如果你转到,你会看到所有代码。这是为了一个简单的计算器。唯一的更改是将retain更改为strong。然后我拖动每个calculator键,用于在owner and control中按下按钮,将数字拖动到计算器上的UITextField。我对此非常陌生。如何启动调试器,以及我将看到什么来告诉我UITextField已设置?感谢所有。从零开始,所有工作正常。可能是错误标记的文件。