views:

8409

answers:

5

Let's say I have the following code:

IBOutlet UITextField* nameTextField;
IBOutlet UILabel* greetingLabel;

I'd like the greetingLabel to read "Hello [nameTextField]" as soon as the user presses any key.

What I need basically is the iPhone equivalent of the Cocoa delegate method -controlTextDidChange:

The -textField:shouldChangeCharactersInRange: delegate method is called each time a keystroke occurs:

- (BOOL)              textField:(UITextField *)textField
  shouldChangeCharactersInRange:(NSRange)range     
              replacementString:(NSString *)string

The string argument returns the character that is pressed. The actual textField's value (nameTextField.text) remains blank however.

What am I missing here? (I'd like nameTextField to reflect the exact string that the user has entered so far)

+8  A: 

you could register an action for the event UIControlEventEditingChanges on the text field:

[nameTextField addTarget:self action:@selector(updateLabelUsingContentsOfTextField:) forControlEvents:UIControlEventEditingChanged];

...

// TODO: error checking

- (void)updateLabelUsingContentsOfTextField:(id)sender {

    greetingLabel.text = [NSString stringWithFormat:@"Hello %@", ((UITextField *)sender).text];

}
Is there a way to specify this using Interface Builder?
Nocturne
Aha.. got it :) Details in my answer below.
Nocturne
+3  A: 

UITextField has a notification UITextFieldTextDidChange which will be fired every time the text changes if you register for it. Just register for that notification and in the method called by the notification, change the label's text.

To add to this, the object passed to your notification handler will have the text of the UITextField.

Hope that helps.

lostInTransit
+15  A: 

It turns out, the easiest way to do this is using Interface Builder:

  • Add a IBAction (to the ViewController, say, as in this case)
  • Ctrl-Click (or right click) on the UITextField in Interface Builder
  • Connect the "Editing Changed" event to the File's Owner's IBAction added in the first step.

Works like a charm :) (I can't believe I spent numerous days on this, and to realize now that the solution was much simpler than I'd thought :P)

Nocturne
A: 

I have a string of allowable characters "0123456789." How do I also allow the backspace from the keyboard? When I implement the code from below the backspace key no longer works. How can I fix this?

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

    return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
Ben Call
Added this to the site as it own separate question... thought I would answer it here as well though. replaced return line with these lines and back button works now:if (range.length == 1){ return string; }else{ return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0); }
Ben Call
return string; // doesn't return a boolean? should be return YES;
cannyboy
A: 

Is there a way to only get the value from the textfield when the enter key is pressed on a bluetooth keyboard (barcode scanner)?

tobias