views:

553

answers:

3

I'm trying to update the username field for a textfield. Currently there is a place holder which has "Username" written in it. I am loading the username from a settings file but for clarity I will just hardcode in the username.

- (void)viewDidLoad {
    settings=[Settings new];
    [settings loadData];

    //username.text=settings.username;
    username.text=@"Test";
}

For some reason this does not work!!! However, if I have a button which when I click it, that will update the textfield.

e.g.

- (IBAction)login:(id)sender{
    username.text=@"test";
}

So the button updates the text but I cannot update from viewDidLoad. Is this a threading issue? I can't believe something so simple is taking so long.

+2  A: 

The controls are probably not created at this point.

Try setting the value in - (void)viewDidAppear:(BOOL)animated in your ViewController.

You might also want to try - (void)viewWillAppear:(BOOL)animated as well.

Xetius
I'm pretty sure that after viewDidLoad the controls will have been created. I can change the placeholder using username.placeholder=@"Test";however username.text=@"Test" does not work.
OK, I just have a vague memory of watching one of the Stanford videos and them mentioning something about load order, initialisation and the framework being lazy with not loading until required.
Xetius
But my memory could well be up the spout!
Xetius
I just had the same problem, I was trying to update the textField from a parent controller but was the value was getting lost. Eventually I hard-wired the viewDidLoad and wrapped the setting of the variable with NSLogs. Each time the value would get logged but it would not appear in the UI. Finally got it to work by setting the textField in viewDidAppear as recommended by Xetius.
Liam
+1  A: 

i don't know why, but you're only able to change the text of a textfield AFTER it was initially displayed

=> use "- (void)viewDidAppear:(BOOL)animated"

this will work

gabtub
A: 

You've declared the text field as an IBOutlet within a property. The mistake I did was that I forgot to connect the Outlets (text fields) to my File's Owner in Interface Builder.

testing