Type in two uitextfield at the same time

走远了吗. 提交于 2019-12-01 14:04:54

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;    
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!