0

Quick Question, I hope,

I have a UITextFeild that I want to type in an number and have that number populate into 6 other UITextfields on the same VC.

The first textfiled is called percentage goal while the others are named endmonth1year1percentage, endmonth2year1percentage, endmonth3year1percentage, etc.

I am currently using iOS6 with storey board.

Any help is appreciated. Thanks.

danypata
  • 8,829
  • 1
  • 28
  • 41
Scubadivingfool
  • 1,215
  • 2
  • 10
  • 21
  • you want to display same number in all the textfields when you enter the text in one textfield right? – Balu May 28 '13 at 14:50
  • Correct. I am just starting out with 6 but later will end up with a little over 100 and don't want to type it all in. – Scubadivingfool May 28 '13 at 14:55

3 Answers3

1

Detect the change in the first text field: UITextField text change event

And then update the text property of other text fields you want to be populated.

Community
  • 1
  • 1
Valent Richie
  • 5,176
  • 1
  • 17
  • 21
0

try like this,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//replace your textfield names here
textField1.text=textField.text;
textField2.text=textField.text;
textField3.text=textField.text;
textField4.text=textField.text;
textField5.text=textField.text;


return YES;
}
Balu
  • 8,440
  • 2
  • 20
  • 41
0

So for the first UITextField you must set the delegate the class that is responsible with the display of the text fields (a view controller or a custom view). For the other UITextFileds you should set a tag like 1,2,3..for each UITextFiled (because you say that will be lots of UITextFields)

After you set the delegate for the first UITextField and setup the tags, you can implement two different delegate methods (depending on what you want).

The method that Sunny provided, this is for instant changes:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
    UITextView *newTextView = (UITextView *)[self.view viewWithTag:i]; 
    //or [yourCustomView viewWithTag:i]
    newTextView.text = textField.text;
  }

  return YES;
}

Second, you can use the following if you want to update the textfields only after the user finished typing and the keyboard is hidden:

-(void)textFieldDidEndEditing:(UITextField *)textField {
  for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
      UITextView *newTextView = (UITextView *)[self.view viewWithTag:i]; 
      //or [yourCustomView viewWithTag:i]
      newTextView.text = textField.text;
  }
}

EDIT

So first of all delegate is a pattern heavy used by iOS here is a tutorial that will explain the basic concept of delegate.

Second, some of the UI controls that iOS provide have a delegate instance (after you read the above tutorial you will understand why and how it's working). A class can be a delegate of a custom UI control only if the class implements the required methods that the delegate provides (NOTE: there are optional methods also in the delegate), if the class doesn't implement the required methods a build warning will be shown at the line where the delegate is set.

Third, the method used in this answer are delegate methods of the UITextFiled (check apple docs)

I lost count, [tag][3] is a property available for all UIViews of subclasses of UIView that can be used to identify an object, but be careful that this property by default is 0 so make sure when you set the tag property you will use a value > 0.

For more details please use google and Apple Docs

danypata
  • 8,829
  • 1
  • 28
  • 41