2009-11-11 91 views
0

我一直在網上搜索很長時間,我無法讓這個工作。在我的文本字段中,當我點擊它,然後按完成或返回它不會消失。我已經完成了每個教程的所有步驟,但仍然無法實現。我在固件3.1.2,但無論如何,這裏是我ViewController.m代碼:返回關鍵文本字段? - iPhone SDK

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    blah.delegate = self; 
    blah.returnKeyType = UIReturnKeyDone; 

} 

- (BOOL)blahShouldReturn:(UITextField *)blah{ 

    [blah resignFirstResponder]; 
    return YES; 
} 

viewcontroller.h:

@interface BlahViewController : UIViewController <UITextFieldDelegate> { 
    IBOutlet UITextField *blah; 

} 

這些都是從文件只是切口。無論如何任何人都可以幫我嗎?當我點擊它,我無法擺脫鍵盤的...

感謝,

凱文

回答

7

我很困惑。當你按下回車按鈕時,你實際上是否期待一種叫做blahShouldReturn:的方法?如果您想使用textFieldShouldReturn:委託方法,則必須將其稱爲textFieldShouldReturn:。您可以使用該方法提供的參數UITextField來確定哪個文本字段正在發送消息。例如:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    if (textField == blah) { 
     [textField resignFirstResponder]; 
    } else if (textField == someOtherTextField) { 
     // Do something else 
    } 
    return YES; 
} 
2

.m文件:

- (void)viewDidLoad { 

[super viewDidLoad]; 

blah.delegate = self; 
blah.returnKeyType = UIReturnKeyDone; 

//Call hideKeyboard action when done button is clicked 
[blah addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit]; 

} 

//Custom hide keyboard action 
- (void)hideKeyboard { 

[blah resignFirstResponder]; 

} 

.h文件中:

//Dont need delegate for this method of hiding keyboard! 
@interface BlahViewController : UIViewController { 
IBOutlet UITextField *blah; 

} 

- (void)hideKeyboard; 

@end