Pierre Moreno
Pierre Moreno

Reputation: 1

UITextField .text attribute always returns an empty string

I'm programming forms for my iOS app, and my UITextFields act strangely :

When the form is in update state (fields are already filled) I can edit and get the new input text (typed by user) with the .text attribute. But when the form is in insert mode the UITextFields are initially empty and I cannot get the text typed then by the user with the .text attribute because it always returns an empty string whatever the user types. Here is a video showing the issue : https://youtu.be/tPJZ3sC-IFU

{
    __weak IBOutlet UITextField *txtLibelle; //linked by ctrl + drag to the storyboard
    NSString *libelleTemp;
}
- (void)viewDidLoad {
[super viewDidLoad];

    // ...

    [txtLibelle addTarget:self
                   action:@selector(editingChanged:)
         forControlEvents:UIControlEventEditingChanged];

    // ...

}

-(void) editingChanged:(id)sender {
    libelleTemp = txtLibelle.text;
    // libelleTemp is set to @"" whatever the user has typed in when I'm in insert mode
}

-(void)initWithLib:(NSString *)lib {
    libelleTemp = [NSString stringWithString:lib];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        txtLibelle.text = libelleTemp;
    }

    // ...

    return cell;
}

The initWithLib: method is called from the previous view controller like this :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *accueilNavSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    FicheAlerteTableViewController *vc = [accueilNavSB instantiateViewControllerWithIdentifier:@"ficheAlerte"];
    NSString *libelle = @"";
    [vc initWithLib:libelle];
    [self.navigationController showViewController:vc sender:self];
}

I already have relinked the field from the storyboard to my class, and in debug mode my UITextField is not nil.

My textfields are in a static UITableView, so I can see if the update worked scrolling the cell off the screen then scrolling it on.

Does anyone know why is this happening and how to solve the problem?

Upvotes: 0

Views: 1852

Answers (3)

bleater
bleater

Reputation: 5499

You are using the wrong action. UITextField's 'text' property will only be set once the editing has ended. So you need to wire your method to the "Editing Ended" event in Interface Builder.

Upvotes: 0

Max Friedman
Max Friedman

Reputation: 445

You are using a UITextField, which can use the UITextFieldDelegate to register editing events and such. See Apple's documentation for more information.

I believe the method you are looking for is:

<UITextFieldDelegate> //link your textField.delegate = self

- (void)textFieldDidEndEditing:(UITextField *)textField {
    libelleTemp = textField.text;
}

Another thing you could try is directly linking an (IBAction) method instead of setting the target programatically. Just control drag to link this method to your text field in IB.

- (IBAction)libelleEndedEditing:(UITextField *)sender {
    libelleTemp = sender.text;
}

Upvotes: 0

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

There is one error in your code. You are not using the correct attribute to load the text into the label.Use This.

This worked for me. I tested this on my Xcode.

-(void) editingChanged:(id)sender {
  //label1.text = field.text;
   libelleTemp=field.text;
    NSLog(@"text is %@",libelleTemp);
   // libelleTemp is set to @"" whatever the user has typed in when I'm in insert mode
}

Upvotes: 1

Related Questions