问题
I want to type in two uitextfield at the same time.Is this possible?
回答1:
You can do it using the following code:
Typein2.h - class where three textFields are declared. The "textFieldBeingEdited" textField is to know which textField (text1 or text2) is being edited
#import <UIKit/UIKit.h>
@interface Typein2 : UIViewController <UITextFieldDelegate> {
UITextField *text1;
UITextField *text2;
UITextField *textFieldBeingEdited;
}
@property (nonatomic, retain) IBOutlet UITextField *text1;
@property (nonatomic, retain) IBOutlet UITextField *text2;
@end
In the viewDidLoad method do the following:
- (void)viewDidLoad
{
[super viewDidLoad];
text1.delegate = self;
text2.delegate = self;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(changeBothFieldsText:)
name:UITextFieldTextDidChangeNotification
object:nil ] ;
}
TextField delegate Method:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if ( [textField isEqual:text1] ) {
textFieldBeingEdited = text1;
}
else
textFieldBeingEdited = text2;
}
changeBothFieldsText Method:
-(void)changeBothFieldsText:(NSNotification*)notification {
if ( [textFieldBeingEdited isEqual:text1] ) {
text2.text = text1.text;
}
else
text1.text = text2.text;
}
回答2:
I see no reason why you can't trap the changes to the text in the first UITextView and then change the text in the second. The notification UITextViewTextDidChangeNotification and the text property should give you what you need.
来源:https://stackoverflow.com/questions/5840473/type-in-two-uitextfield-at-the-same-time