如果我要在第一个UITextField中输入文本,该文本会自动显示在第二个UITextField中。在我的情况下,我已经尝试了所有UITextFieldDelegate方法,但是如果我在第一个UITextField中输入一个,我在第二个UITextField中得到一个单独的,那么我就会变得比一个字符少。
但是我需要它,就像我要去ABCDEF类型那样...在UITextFielD 1 UITextField 2中它也应该像ABCDEF一样打印.....输入应该是继续文本也应该继续。
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UITextField2.text = UITextField1.text;
UITextField.text = textField.text;
}
我试过这样但没有成功。在此先感谢。
答案 0 :(得分:1)
试试这个..
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField isEqual:UITextField1]){
UITextField2.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
return YES;
}
答案 1 :(得分:0)
使用的对象:
UITextField *txtFFirst;
txtFFirst
中的更改传播到txtFSecond
UITextField *txtFSecond;
可能的方式:
UITextFieldTextDidChangeNotification
通知UIControlEventEditingChanged
控制事件- (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];
}
答案 2 :(得分:0)
我使用不同的技术来处理shouldChangeCharactersInRange:
中的文本更改,而不是使用UITextField
。它类似于textViewDidChange:
的{{1}}方法。
使用以下方法添加行为:
UITextView
然后在目标方法中:
[textField1 addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
那就是它。除了避免使用观察者之外,这里的优点是你在实际更改之后处理文本更改。
答案 3 :(得分:0)
尝试此代码:
#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