2012-02-09 44 views

回答

4

步驟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]; 
+0

感謝您的建議,它的工作原理。 – 2012-02-09 09:16:40

0

新增通知創建代碼

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField]; 

和實施

- (void) changeText: (id) sender; 
{ 
    if ([textField.text length] == 1) 
    { 
     [textField resignFirstResponder]; 
    }   
} 
+0

感謝您的幫助,但我想wihout按壓完成按鈕 – 2012-02-09 07:14:51

0

我想這是要你正在尋找?

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    [add your method here]; 
    return YES; 

} 

或者,如果你想它開始編輯,你可以把這個代碼textFieldDidBeginEditing其儘快辭職:委託方法

[textField resignFirstResponder]; 

檢查此鏈接

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

1

你必須用文本代理方法編寫代碼

- (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]; 
} 

} 
+0

感謝您的幫助,只要我在文本框只輸入一個字符,返回的鍵盤,但我想,只要我只輸入一個字符返回鍵盤在textfield沒有按下完成按鈕 – 2012-02-09 07:14:33

+0

然後編寫代碼在shouldChangeCharactersInRange:(NSRange)範圍replacementString:(NSString *)字符串它檢查一個接一個字符 – Hiren 2012-02-09 07:23:09

0
- (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 
    } 

} 
相關問題