回答
步驟1:創建實現協議UITextFieldDelegate類
@interface TheDelegateClass : NSObject <UITextFieldDelegate>
步驟2:在你的實施方式中,覆蓋的方法 - (BOOL )textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString :(NSString *)string
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// newString is what the user is trying to input.
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([newString length] < 1) {
// If newString is blank we will just ingore it.
return YES;
} else
{
// Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
// And make the keyboard disappear.
[textField resignFirstResponder];
// Return NO to not change text again as we've already changed it.
return NO;
}
}
第3步:將委託類的實例設置爲UITextField的委託。
TheDelegateClass *theDelegate = [[TheDelegateClass alloc] init];
[theTextField setDelegate:theDelegate];
新增通知創建代碼
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];
和實施
- (void) changeText: (id) sender;
{
if ([textField.text length] == 1)
{
[textField resignFirstResponder];
}
}
感謝您的幫助,但我想wihout按壓完成按鈕 – 2012-02-09 07:14:51
我想這是要你正在尋找?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[add your method here];
return YES;
}
或者,如果你想它開始編輯,你可以把這個代碼textFieldDidBeginEditing其儘快辭職:委託方法
[textField resignFirstResponder];
檢查此鏈接
你必須用文本代理方法編寫代碼
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
,然後檢查你的字符串的長度在textFieldDidBeginEditing
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
}
感謝您的幫助,只要我在文本框只輸入一個字符,返回的鍵盤,但我想,只要我只輸入一個字符返回鍵盤在textfield沒有按下完成按鈕 – 2012-02-09 07:14:33
然後編寫代碼在shouldChangeCharactersInRange:(NSRange)範圍replacementString:(NSString *)字符串它檢查一個接一個字符 – Hiren 2012-02-09 07:23:09
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if([textField.text length]==1)
{
// here perform the action you want to do
}
}
- 1. 鍵盤輸入返回鍵
- 2. iPhone鍵盤的返回鍵將光標移動到下一個文本域
- 3. 在文本框中輸入密碼時鍵入的字符
- 4. 更改輸入時,鍵盤不接受第一個字符
- 5. XNA - 鍵盤文本輸入
- 6. 返回在Java中輸入爲反向文本的字符串
- 7. 在一個文本框中輸入一個數字在回車鍵上按
- 8. 強制用戶在輸入文本字段中輸入英文鍵盤
- 9. 如何在文本文件中記錄每個鍵盤輸入?
- 10. 在Python中超時的鍵盤輸入
- 11. 二維文本框的鍵盤輸入
- 12. 在記事本++中禁用鍵盤快捷鍵的控制字符輸入
- 13. 數字鍵盤鍵盤不顯示返回鍵中的iPhone
- 14. 如何在文本字段上返回鍵盤,同時在ios中的tableview單元上的文本字段
- 15. 在輸入第一個字符時,軟鍵盤向上推動佈局
- 16. 更改鍵盤輸入文字的Android
- 17. Java:鍵盤類輸入字符串
- 18. 在文本框中輸入文本後禁用鍵盤
- 19. 黃瓜:如何從鍵盤輸入文本到文本字段?
- 20. 在AS3中輸入文本字段中輸入按鍵輸入
- 21. 用鍵盤快捷鍵替換特殊字符實時輸入
- 22. 獲取從鍵盤(或自定義鍵盤)輸入的文本
- 23. 返回一個隨機文本字段輸入
- 24. 如何在點擊輸入選擇一個文本框的鍵盤輸出
- 25. 在用戶輸入一個字符之前禁用回車鍵?
- 26. Android鍵盤隱藏文本輸入
- 27. Java返回輸入文本
- 28. 處理數字鍵盤的藍牙鍵盤的返回鍵
- 29. 顯示數字鍵盤在iPhone上使用輸入文本
- 30. 在java中的字符串和鍵盤輸入
感謝您的建議,它的工作原理。 – 2012-02-09 09:16:40