UITextfield.text returns null [closed]
Asked Answered
G

4

6

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

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

in implementation file and

@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

    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.

Gerent answered 3/12, 2011 at 4:0 Comment(5)
Try printing the numberDisplay variable instead of it's text property. I do suspect that somewhere else you haven't initialized your text field; as you can't assign a variable to a null object you get null in return.Triable
how would you initialize the text field. In attributes inspector i have tried setting the text and/or placeholder and both to 0 to see if that would help but it doesn'tGerent
You must link up the UiTextView in your .xib file with the IBOutlet.Sire
I've dragged the text box in the xib file to the outlet and it does register, since in the connection inspector it shows the referenceing outlet being the file's owner IBOUtlet i created.Gerent
Everyone is assuming that this UITextField is not connected correctly, and this is likely true, but you should test for that. That can be done by adding a third line: NSLog(@"the text field is %@",numberDisplay);Warila
E
2

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

Eartha answered 3/12, 2011 at 4:13 Comment(2)
how can the text field be null if i set it to "a"Gerent
To demonstrate his answer that nil can respond to any message with nil.Sire
C
1

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

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.

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
Cchaddie answered 3/12, 2011 at 4:10 Comment(0)
P
0

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

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

Petrinapetrine answered 3/12, 2011 at 4:12 Comment(0)
M
0

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

@property (nonatomic, retain) UITextField *numberDisplay;

and add the line in .m

@synthesize numberDisplay;

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

Mutable answered 3/12, 2011 at 5:35 Comment(3)
check whether your connections in IB are appropriate or nor..Mutable
I've dragged the text box in the xib file to the outlet and it does register, since in the connection inspector it shows the referenceing outlet being the file's owner IBOUtlet i created.Gerent
First drag the UITextField from library to View and then Ctrl-drag from file's owner to textField in View and then select the textFiled declared in .h file. NOw,save and run..Hope this time it will workMutable

© 2022 - 2024 — McMap. All rights reserved.