2017-08-02 135 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,每個都有自己的鍵盤類型。

然後在您的最後,您可以將輸入的字符串附加到一起,以執行您需要執行的任何操作。這是更好的用戶體驗,也更簡單。