返回关键文本字段? - iPhone SDK

时间:2009-11-11 18:49:51

标签: objective-c cocoa-touch

我已经在网上搜索了很长时间,我无法让它发挥作用。在我的文本字段中,当我点击它然后按完成或返回它不会消失。我已经完成了每个教程的所有步骤,但我仍然无法让它工作。我在固件3.1.2上,但无论如何这里是我的ViewController.m中的代码:

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

}

这些只是从文件中删除。无论如何,有人可以帮助我吗?我点击它时无法摆脱键盘......

谢谢,

凯文

2 个答案:

答案 0 :(得分:7)

我很困惑。当你按下Return按钮时,你真的期待一个名为blahShouldReturn:的方法被调用吗?如果要使用textFieldShouldReturn:委托方法,则必须将其称为textFieldShouldReturn:。您可以使用该方法提供的UITextField参数来确定发送消息的文本字段。例如:

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

答案 1 :(得分: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