iOS Textfield control keyboard and pop-up box

8436312-946ca757eea25ffb.png
Simulator Screen Shot - iPhone 8 - 2019-06-01 at 16.02.02.png

Problem Description:

Textfield controls residence for display, default is editable textfield, when RESIDENCE information you want is to display the address selector, but the default keyboard will pop up, and also blocking the selector.
解决思路!It is to make textfield settings can not be edited, but to be able to respond to a click event, but also to have opened the keyboard closed.

UITextFieldDelegate

//TextField是否可编辑
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == 10) {
     // 此处 self.view 是viewController 的view,关闭键盘
       [self.view endEditing:YES]; 
        [self AddressTF:textField]; //弹出地址选择器
        return NO ;//当前 textField 不可编辑,可以响应点击事件
    }else{
        return YES;//可编辑
    }
}
//地址选择器
- (void)AddressTF:(UITextField *) textField
{

}

UITextField way to sum up the relevant agents and conventional operation

Sometimes when you open the search interface, like a pop-up keyboard to enter the interface

//使textField 成为第一响应者
 [textField becomeFirstResponder];
//取消第一响应者
 [textFiled resignFirstResponder];

return key, and is usually recovered return keyboard

   textField.returnKeyType = UIReturnKeyJoin;

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

Enter the call at the beginning of the text entry box

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    //将键盘弹出
    NSLog(@"开始输入");
}

Call the end of the input text entry box

- (void)textFieldDidEndEditing:(UITextField *)textField{
    //获取当前文本输入框中所输入的文字
    NSLog(@"所输入的内容为:%@",textField.text);
    //例:判断账号书写形式是否正确 如果不正确提示填写错误 重新输入
    NSLog(@"结束输入");
}

That would change the method of text entry box call content occur

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    /*
    NSLog(@"内容:%@",textField.text);//获取的是上一次所输入内容
    NSLog(@"Location:%lu Length:%lu",range.location,range.length);//范围为当前文字的位置,长度为零
    NSLog(@"==%@==",string);//实时获取当前输入的字符

    */
    //需求 实时获取当前文本框中的所有文字

    NSString * resultStr = [textField.text stringByAppendingString:string];

    NSLog(@"%@",resultStr);

    //可在该方法中判断所输入文字是否正确

    return YES;
}

Reproduced in: https: //www.jianshu.com/p/7fb62c2bc940

Guess you like

Origin blog.csdn.net/weixin_34183910/article/details/91101126