Sean Wentz
Sean Wentz

Reputation: 51

UITextField settext not working

This is frustrating the hell out of me. I am a beginner programmer and cannot figure out why the text is not being changed.

Here is my method which is supposed to set the text of a UITextField:

-(void)updateDays:(NSInteger)days
{
    NSString* daysString = [[NSString alloc] initWithFormat:@"%d", days];
    [daysTextField setText:daysString];
    [daysString release];
}

For whatever reason nothing is happening.

Help appreaciated!

Upvotes: 4

Views: 10193

Answers (4)

Yunus Nedim Mehel
Yunus Nedim Mehel

Reputation: 12369

I have experienced this several times, but I think all of them was reasoned by that the UITextField was not initialized at that point.

Set a breakpoint and use debugger to make sure that your UITextField is not nil. You should also check the connection between the .xib file and your code.

Upvotes: 4

Jian
Jian

Reputation: 101

I meet this problem too .

It's my code:(in viewdidload)

UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,50)]];
tv.text = @"12345";
tv.textColor=[UIColor blackColor];
[self.view addSubView:tv];

And the text(12345) doesn't show;

Lastly,I found that when i set textColor to gray,or any color not black,it works;

I think that it's a bug of the simulator.

I use xcode 4.2 and the iphone5.0simulator.

Upvotes: 0

mackworth
mackworth

Reputation: 5953

Anytime you have a frustration along the lines of "why isn't this line working", use the debugger or just add an NSLog before it to print out the relevant data:

NSLog(@"updateDays: %@ %@ <= %@", daysTextField, daysTextField.text, daysString);

Then you know the line (a) is getting executed, (b) the variables are the ones you think they are, and (c) the values are reasonable.

Upvotes: 6

visakh7
visakh7

Reputation: 26390

Check if the method is called. If yes check if the textfield is set up properly. Cross check to see if the IBOutlet connections are made to the correct object.

Upvotes: 0

Related Questions