2017-08-02 102 views
0

我是新来iOS的UITextField更改键盘

我想改变留在UITextField某些字符后,键盘,

为如:我有@"AEBPQ1234M"

字符串第5个字母我想字母键盘,然后为接下来的4个字母我需要数字键盘和最后一个字母,我需要再次字母表。

这里是我尝试过的代码,但是当我在键盘中弹出退格键时它不工作。

-(void)textFieldDidChange :(UITextField *)theTextField{ 
    if (theTextField.text.length < 5 || theTextField.text.length >= 9) { 
    [theTextField setKeyboardType:UIKeyboardTypeEmailAddress]; 
} 

if (theTextField.text.length >= 5 && theTextField.text.length < 9) { 
    [theTextField setKeyboardType: UIKeyboardTypeNumberPad]; 
} 

[theTextField reloadInputViews]; 
} 

我在哪里犯错?

+0

在其他情况下工作?,尝试使用'textfieldshouldchangecharactersinrange'委托 –

+1

这并不是理想的方式。你不应该改变键盘。这不是很好的用户体验。 –

+0

可以请你帮我拿到代码我是新来的iOS –

回答

0

尝试这个 -

-(void)textFieldDidChange :(UITextField *)theTextField{ 
    if (panNumber.length <= 5 || panNumber.length > 9) { 
     [theTextField setKeyboardType:UIKeyboardTypeEmailAddress]; 
    } 
    else { 
     [theTextField setKeyboardType: UIKeyboardTypeNumberPad]; 
    } 


[theTextField reloadInputViews]; 
} 
0

使用本

其中selectortextEditingChanged:应该监听UIControlEventEditingChanged事件

//添加按照viewDidLoad

[self.textField addTarget:self action:@selector(textEditingChanged:) forControlEvents:UIControlEventEditingChanged]; 

-(void)textEditingChanged:(UITextField*) sender{ 
    if (sender.text.length > 5 && sender.text.length < 9) { 
     [self.textField setKeyboardType: UIKeyboardTypeNumberPad]; 
    } 

    if (sender.text.length < 5 || sender.text.length == 10) { 
     [self.textField setKeyboardType:UIKeyboardTypeEmailAddress]; 
    } 

    [self.textField reloadInputViews]; 
} 

这将是一个坏EXP对于在电话键盘中键入速度非常快的用户来说,

+0

不按我的要求工作 –

+0

它是如何为你工作的? – 2017-08-02 12:52:43

+0

@Appledeveloper是这种方法'textEditingChanged'连接到UITextView使用故事板? – 2017-08-02 12:54:08

0

请尝试以下代码。

首先在您的.h文件中声明UITextFieldDelegate。同时设置它的代表与自我。

您可以设置自委托有两种类型: -
1.故事板
2.代码

当代码写在textField.delegate = self;方法viewDidLoad设置。

现在使用下面的代码.m文件

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 

    if (textField.text.length < 4 || textField.text.length > 7) { 

     [textField setKeyboardType: UIKeyboardTypeEmailAddress]; 

    } else { 

     [textField setKeyboardType: UIKeyboardTypeNumberPad]; 
    } 

    [textField reloadInputViews]; 
    return YES; 
} 
0

请检查我answer.It工作perfectly.I试过那么只有我在这里发表了答案。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 

    if(string.length == 0 && range.location == 9) 
     range.location = range.location - 2; 

    if(string.length == 0 && range.location == 8) 
     range.location = range.location - 1; 

    if(string.length == 0 && range.location > 3) 
     range.location = range.location - 2; 

    if(range.location <= 3){ 
     textField.keyboardType = UIKeyboardTypeAlphabet; 
     [textField reloadInputViews]; 
     return YES; 
    } 
    else if(range.location > 3 && range.location <= 7){ 
     textField.keyboardType = UIKeyboardTypeNumberPad; 
     [textField reloadInputViews]; 
     return YES; 
    } 
    else if(range.location > 7){ 
     textField.keyboardType = UIKeyboardTypeAlphabet; 
     [textField reloadInputViews]; 
     return YES; 
    } 
    else{ 
     return NO; 
    } 
    return YES; 
} 
+0

当我们前进时,它的工作,但是当我回退或我切一个字符它不工作:( –

0

我建议使用多个textField,每个都有自己的键盘类型。

然后在您的最后,您可以将输入的字符串附加到一起,以执行您需要执行的任何操作。这是更好的用户体验,也更简单。