Question

If I am going to enter text in first UITextField that automatically should display in second UITextField. In my case I already tried with all UITextFieldDelegate methods but if I am entering a in first UITextField I am getting a alone in second UITextField, I am getting lesser then one character.

But I Need it like if I am going type ABCDEF..... IN UITextFielD 1 IN UITextField 2 also it should be print like ABCDEF.....typing should be continued text also should be continued.

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
 UITextField2.text = UITextField1.text;

UITextField.text = textField.text;
}

I tried like this but not worked out. Thanks in Advance.

Was it helpful?

Solution

Try this..

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField isEqual:UITextField1]){
        UITextField2.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    }
    return YES;
}

OTHER TIPS

Instead of using shouldChangeCharactersInRange:, I use a different technique to handle text changes in UITextFields. It is similar to the textViewDidChange: method of UITextView.

You add behaviour using:

 [textField1 addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];

Then in the target method :

- (void) textFieldDidChange {

    textField2.text = textField1.text;
}

That's it. The advantage here, besides avoiding observers, is that you handle text changes after it actually changes.

Objects used:

  1. UITextField *txtFFirst;
    • changes in txtFFirst propagate to txtFSecond
  2. UITextField *txtFSecond;

Possible ways:

  1. UITextFieldTextDidChangeNotification notification
    or...
  2. UIControlEventEditingChanged control event

- (void)viewDidLoad
{
    //...

    //[1] Notification Method
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidChangeByNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:txtFFirst];

    //OR... (uncomment the following and comment the above)

    //[2] Control Event Method
    //[txtFFirst addTarget:self
    //              action:@selector(textFieldDidChangeByControlEvent:) 
    //    forControlEvents:UIControlEventEditingChanged];
}

//[1] Fires when the Notification Method is used
-(void)textFieldDidChangeByNotification:(NSNotification *)note
{
    UITextField *txtFTemp = [note object];
    [txtFSecond setText:txtFTemp.text];
}

//[2] Fires when Control Method is used
-(void)textFieldDidChangeByControlEvent:(UITextField *)sender
{
    [txtFSecond setText:sender.text];
}    

Try This code:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic,strong) IBOutlet UITextField *name;
@property (nonatomic,strong) IBOutlet UITextField *sameName;

@end

#import "ViewController.h"

@interface ViewController ()

@end


@implementation ViewController
@synthesize name,sameName;

- (void)viewDidLoad
{
    name.delegate = self;
    sameName.delegate = self;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField isEqual:name]){
    sameName.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    }
    return YES;
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top