Notes on the agent of iOS UITextField

Keywords: iOS

Today, when I was working on a project, there was a need to click the button to input the text in a specific edit box. At the beginning, I also wrote in the idea of C + +. First, I got the position of the cursor, and then I was judging whether it was in that edit box for input. Later, my colleague next to me saw that I was directly taught to use the proxy method, because it was not long since I contacted iOS, and I was not clear about the usage of < uitextfielddelegate >. Thank you very much to my colleagues.

1. Agent < uitextfielddelegate >

@interface idiom_ViewController ()<UITextFieldDelegate>
{
    UITextField * _selectTf;
    NSArray *UIButton_array;
}
@property (weak, nonatomic) IBOutlet UITextField *first_idiom;
@property (weak, nonatomic) IBOutlet UITextField *second_idiom;
@property (weak, nonatomic) IBOutlet UITextField *third_idiom;
- (void)viewDidLoad {
    [super viewDidLoad];
    //Protocol to implement UITextFieldDelegate
    _first_idiom.delegate=self;
    _second_idiom.delegate =self;
    _third_idiom.delegate =self;
    //Click the edit box to hide the soft keyboard
    _first_idiom.inputView =[UIView new];
    _second_idiom.inputView =[UIView new];
    _third_idiom.inputView =[UIView new];
    //Create gesture recognition objects and listen for gestures
    UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [self.view addGestureRecognizer:tap];
    // Do any additional setup after loading the view from its nib.
}
//Lose focus
-(void)tapAction{
    [self.view endEditing:YES];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    _isBeginTf =NO;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    _isBeginTf =YES;
    _selectTf =textField;
}

2. Button click event - edit box input button text

- (void)button_word:(UIButton *)btn
{
    if (!_isBeginTf) {
        return;
    }
    _selectTf.text = [NSString stringWithFormat:@"%@%@",_selectTf.text,btn.titleLabel.text];
    btn.userInteractionEnabled =NO;
    btn.backgroundColor =[UIColor lightGrayColor];
}

3. Delete button event

- (IBAction)goBackButtonAction:(id)sender {
    if (!_isBeginTf) {
        return;
    }
    
    if ([_selectTf.text isEqualToString:@""]) {
        return;
    }
    //Get the last text of the edit box
    NSString *gaBackStr =[_selectTf.text substringWithRange:NSMakeRange(_selectTf.text.length-1, 1)];
    //Get edit box length -1 Text
    _selectTf.text =[_selectTf.text substringToIndex:_selectTf.text.length -1];
    
    for (int i=0; i<12; i++) {
         UIButton *btn= UIButton_array[i];
        //Determine whether the deleted text is the same as the text in the button
        if ([btn.titleLabel.text isEqualToString:gaBackStr]) {
            //Same, the button changes from non clickable to clickable, and the color changes
            btn.userInteractionEnabled =YES;
            btn.backgroundColor =[UIColor orangeColor];
            return;
        }
    }
    
}

Posted by n00854180t on Sun, 01 Dec 2019 12:57:04 -0800