简体   繁体   English

textFieldShouldReturn不起作用(在XCode 4上使用故事板)

[英]textFieldShouldReturn doesn't work (using storyboard on XCode 4)

I have a button and a textfield. 我有一个按钮和一个文本字段。 I just want keyboard disappear when clicking on button. 我只是想在点击按钮时键盘消失。 Why my code below doesn't work. 为什么我的代码不起作用。

Update: I saw something about file owner. 更新:我看到了文件所有者的一些事情。 I don't understand how to do this in XCode4 I use storyboard and I can't see any file owner icon. 我不明白如何在XCode4中执行此操作我使用故事板,我看不到任何文件所有者图标。

Update 2: I found a tut http://www.techotopia.com/index.php/Writing_iOS_4_Code_to_Hide_the_iPhone_Keyboard_%28Xcode_4%29 but it uses XIB file on XCode 4 not storyboard. 更新2:我发现了一个问题http://www.techotopia.com/index.php/Writing_iOS_4_Code_to_Hide_the_iPhone_Keyboard_%28Xcode_4%29但它在XCode 4上使用XIB文件而不是故事板。 How to do this with storyboard ? 如何用故事板做到这一点?

myViewController.h myViewController.h

@interface myViewController : UIViewController <UITextFieldDelegate>
{
    UITextField *myTextField;
}

@property (retain, nonatomic) IBOutlet UITextField *myTextField;

myViewController.m myViewController.m

- (BOOL)textFieldShouldReturn: (UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    myTextField.delegate = self;
}

- (IBAction)DoCalc:(id)sender {
      // ...
}

Check to see if the textFieldShouldReturn function that you've written into myViewController.m is even being called. 检查你写入myViewController.m的textFieldShouldReturn函数是否被调用。 Set a breakpoint inside of it and then run the simulator. 在其中设置断点,然后运行模拟器。 Press the return key on the keyboard. 按键盘上的返回键。 If the program doesn't break, then the function you've written isn't being called. 如果程序没有中断,则不会调用您编写的函数。

If it is, it's because you haven't delegated UIResponder duties to your view controller. 如果是,那是因为您没有将UIResponder职责委托给您的视图控制器。 Make sure you are delegating responder duties to your myViewController class for the UITextField that you're working with. 确保您将响应者职责委派给您正在使用的UITextField的myViewController类。

In storyboard, you do this by control-dragging from the UITextField widget to the yellow orb below the scene, and selecting "delegate" from the context menu that appears. 在storyboard中,您可以通过控制从UITextField小部件拖动到场景下方的黄色圆球,然后从显示的上下文菜单中选择“委托”来完成此操作。

textFieldShouldReturn: should return NO to hide the keyboard. textFieldShouldReturn:应该返回NO以隐藏键盘。 One more thing - do not set first responder to self , [textField resignFirstResponder] is enough, iOS should figure nextResponder by itself. 还有一件事 - 不要将第一响应者设置为self[textField resignFirstResponder]就足够了,iOS应该自己计算nextResponder

You can hide the keybord in this method... 你可以在这个方法中隐藏keybord ......

- (void)textFieldDidEndEditing:(UITextField *)textField {

     [textField resignFirstResponder];
}

Remove [self becomeFirstResponder]; 删除[self becomeFirstResponder]; should do it. 应该这样做。

resignFirstResponder is to dismiss the keyboard. resignFirstResponder是解雇键盘。 while becomeFirstResponder is to open the keyboard. becomeFirstResponder则是打开键盘。 so in your code, you are closing then opening again the keyboard at the same time. 所以在你的代码中,你正在关闭然后再次打开键盘。 The last action is opened the keyboard, hence it won't close. 最后一个动作是打开键盘,因此它不会关闭。

you may look into UIResponder Class as superclass of UITextField 您可以将UIResponder类视为UITextField超类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM