-1

I need to pass UITextField data to a UILabel at that same time a text is entered into a textfield.

Ex: If I type in "$10." in the textfield, it should instantly show up on the UILabel without pressing any button.

Essentially I want the price to be "$10.55", but you know what I mean.

@property (weak, nonatomic) IBOutlet UITextField *PriceTextField;
@property (nonatomic, strong) NSString *PriceString;
@property (strong, nonatomic) IBOutlet UILabel *PriceLabel;
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Machina
  • 252
  • 2
  • 13
  • What part of this do you need help with? Do you know how to handle the event sent each time the text field is changed? Do you know how to update a label? Both of these task can be found with a little searching. – rmaddy Sep 12 '14 at 04:38
  • you can update UILabel text with UITextFieldDelegate – Huy Nghia Sep 12 '14 at 04:39

3 Answers3

1

You can achieve it through implementing the shouldChangeCharactersInRange delegate.

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

   @try{

      self.PriceLabel.text =  [textField.text stringByReplacingCharactersInRange:range withString:string];
   }
   @catch (NSException *exception){

      NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
   }
   @finally{

      return YES;
   }
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
1

Edit: Have a look at @rmaddy's comment to this, as it is the nicest, cleanest way to go about it.


Unfortunately there's no part of the UITextFieldDelegate informing you every time the textfield changes, but you can use the name:UITextFieldTextDidChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateLabelFromTextField:)
                                             name:UITextFieldTextDidChangeNotification
                                           object:nil];

Put this in you viewDidLoador wherever it makes sense. Then simply:

- (void)updateLabelFromTextField:(NSNotification *)notification{
    If (notification.object == self.PriceTextField){
        UITextField *textField = (UITextField *) notification.object;
        self.PriceLabel.text = textField.text;
    }
}
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
  • 2
    This is done much more easily with `[someTextField addTarget:self action:@selector(textFieldChangedAction:) forControlEvents:UIControlEventEditingChanged];`. No need for notifications. Just implement `- (void)textFieldChangedAction:(UITextField *)textField`. – rmaddy Sep 12 '14 at 05:17
  • BTW - Don't forget that you also need to remove the notification observer when the view controller is done (assuming you use the notification solution). – rmaddy Sep 12 '14 at 05:29
-1

implement this UITextField delegate method and update your label in it

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

    self.label.text =  [textField.text stringByAppendingString:string];

    return YES;
}
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • 2
    This is not what the OP wants. This sets the label to the last character typed. – rmaddy Sep 12 '14 at 04:43
  • This may not work when the back-space keys are applied. – Shamsudheen TK Sep 12 '14 at 04:47
  • for handling a backspace, have a look at [this](http://stackoverflow.com/a/2654410/1219956) – Fonix Sep 12 '14 at 04:54
  • @rmaddy, i see the error of my ways, this method doesnt account for if the user is typing not at the last position of the entered text – Fonix Sep 12 '14 at 05:05
  • No. The problem is that `string` is the most recently typed or pasted text regardless of where it is being inserted. If the text field currently has "ABC" and the user types "D", your code would set the label to "D" and not "ABCD". See the other answer for a more correct solution. – rmaddy Sep 12 '14 at 05:07
  • Oops - It's getting late. It's still has lots of problems. It doesn't handle the user tapping Delete. It doesn't handle the user cutting text. It doesn't handle any paste or key press when the cursor is anywhere but at the end or if any text is selected. Again, see Ramshad's answer for the proper way to calculate the new text value. – rmaddy Sep 12 '14 at 05:38