xcode UITextfield.text 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8365425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:39:26  来源:igfitidea点击:

UITextfield.text returns null

objective-ciosxcodeios-4.2

提问by stephen kronwith

Assume there's code for an iphone project that is :

假设有一个 iphone 项目的代码:

      IBOutlet UITextField* numberDisplay;
      @property (strong,nonatomic) IBOutlet UITextField *numberDisplay;

in implementation file and

在实现文件和

@synthesize numberDisplay;

@synthesize numberDisplay;

in implementation file. Also in implementation is

在实现文件中。也在实施中

     -(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 ];  
    }

   }

When I run the app there is no display shown in the UITextfield, even though the connections were made with IB and is proven to be made by viewing the inspector. Even as a test if I add the lines

当我运行该应用程序时,UITextfield 中没有显示任何显示,即使连接是使用 IB 建立的并且通过查看检查器证明是建立的。即使作为测试,如果我添加行

    numberDisplay.text = @"a";
    NSLog(@"the value is %@",numberDisplay.text);   
    NSLog(@"the value is %@",numberDisplay);

I get " the value is (null) in both cases.Any ideas.

我得到“两种情况下的值都是(空)。任何想法。

Can someone please tell me what is wrong with these two lines?. Thank you.

有人可以告诉我这两行有什么问题吗?谢谢你。



Thanks to all. I started from scratch and now all works. Looks like I had a mislabeled file.

谢谢大家。我从头开始,现在一切正常。看起来我有一个错误标记的文件。

回答by Dani

Null objects can receive selectors and they ignore them and return null object. the case you encounter is like this:

空对象可以接收选择器,它们会忽略它们并返回空对象。你遇到的情况是这样的:

[(UITextField*)nil setText:@"a"];
NSLog(@"the value is %@", [(UITextField*)nil text]);

Make sure the text field is not null

确保文本字段不为空

回答by alloc_iNit

Following line will return non null value if you have set IBOutlet to Interface builder. It will declared in .h file.

如果您已将 IBOutlet 设置为 Interface builder,以下行将返回非空值。它将在 .h 文件中声明。

IBOutlet UITextField* numberDisplay;

If you are not setting outlet to an IB, it will obviously return null value or you have to initialize it by programmatically.

如果您没有将插座设置为 IB,它显然会返回空值,或者您必须以编程方式对其进行初始化。

UITextField *numberDisplay = [[UITextField alloc] initWithFrame:CGRectMake(10,10, 100,30)];
numberDisplay.font = [UIFont fontWithName:@"Verdana" size:12.0];
numberDisplay.background = [UIColor clearColor];
numberDisplay.text = @"123456789";
[self.view addSubview:numberDisplay];
NSLog(@"the value is %@",numberDisplay.text); // returns 123456789

回答by Patrick Perini

It looks like you might not be properly setting your IBOutletsin Interface Builder.

看起来您可能没有IBOutlets在 Interface Builder 中正确设置。

Check out my answer to this question, regarding basically the same thing.

查看我对这个问题的回答,关于基本相同的事情。

回答by vipul

you have to synthesize the textfield properly in following ways:- Add the line in .h

您必须通过以下方式正确合成文本字段:- 在 .h 中添加该行

@property (nonatomic, retain) UITextField *numberDisplay;

and add the line in .m

并在 .m 中添加该行

@synthesize numberDisplay;

and then do what you did. You will not get the null value this time.

然后做你所做的。这次你不会得到空值。