UITextfield.text returns null

Assume the code for the iphone project:

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

in the implementation file and

@synthesize numberDisplay;

in the implementation file. Also in implementation

  -(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 launch the application, the display is not displayed in the UIText field, although the connections were made with IB, and it was proved that they were made by looking at the inspector. Even as a test, if I add lines

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

I get a "null" value in both cases. Any ideas.

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


Thanks to everyone. I started from scratch and now everything works. It looks like I had a file with the wrong label.

+6
source share
4 answers

Null objects can accept a selector, and they ignore them and return a null object. The case you are facing is as follows:

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

Make sure the text field is not null

+2
source

The next line will return a non-zero value if you set IBOutlet in Interface Builder. It will be declared in the .h file.

 IBOutlet UITextField* numberDisplay; 

If you do not install the socket on IB, it will obviously return a zero value, or you must initialize it programmatically.

 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 
+1
source

It looks like you might have incorrectly configured IBOutlets in Interface Builder.

See my answer to this question , basically in the same.

0
source

You must synthesize the text box correctly in the following ways: Add a line to .h

 @property (nonatomic, retain) UITextField *numberDisplay; 

and add the line to .m

 @synthesize numberDisplay; 

and then do what you did. This time you will not get a null value.

0
source

Source: https://habr.com/ru/post/902980/


All Articles