2012-03-09 72 views
0

這是我的代碼:爲什麼文本字段不能響應隱藏鍵盤?

- (BOOL) textFieldShouldReturn:(UITextField *)textField { 
    [txtSiteDesc resignFirstResponder]; 
    [txtDesc resignFirstResponder]; 
    [ssFS resignFirstResponder]; 

    return YES; 

}

這是.h文件:

#import <UIKit/UIKit.h> 

@interface slEnterDataViewController : UITableViewController <UITextFieldDelegate> { 

    UITextField *txtSiteDesc; 
    UITextField *txtDesc; 
    UITextField *ssFS; 
} 

@property (nonatomic, retain) IBOutlet UITextField *txtSiteDesc; 
@property (nonatomic, retain) IBOutlet UITextField *txtDesc; 
@property (nonatomic, retain) IBOutlet UITextField *ssFS; 

@end 

它的工作原理爲txtSiteDesc,而不是任何其他人。我假設問題出在textFieldShouldReturn方法中;我想我可以檢查「textField」的值來查看它是哪個字段,然後執行適當的「resignFirstResponder」並返回。我很接近(我認爲)但還不夠接近。

幫助將不勝感激。 :D

回答

0

是否有可能已經爲txtSiteDesc設置了委託,但不能爲其他兩個文本字段設置代理?這可以解釋爲什麼textFieldShouldReturn:被稱爲您的第一個文本字段而不是其他字段。確保您將所有三個文本字段的delegate屬性設置爲self(其中self =您的視圖控制器)。

+0

非常感謝你......現在就像一個冠軍!我感謝你的幫助...:D – SpokaneDude 2012-03-10 02:33:46

0

即使我面臨這個問題。這是你在找什麼:

- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    if([string hasSuffix:@"\n"]) 
    { 
     [theTextField resignFirstResponder]; 
     return NO; 
    } 
    return YES; 
} 

這將隱藏鍵盤,當你按回車鍵/鍵盤上完成等。