iOS Xcode 键盘完成按钮功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10039367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:43:59  来源:igfitidea点击:

iOS Xcode keyboard done button function

objective-ciosxcodekeyboard

提问by Kee Yen Yeo

I just can't make the "Done" button to quit the keyboard.

我只是无法让“完成”按钮退出键盘。

I used this in my controller.h file

我在我的 controller.h 文件中使用了这个

- (IBAction)textFieldDoneEditing:(id)sender;

and this for my controller.m file

这是我的 controller.m 文件

- (IBAction)textFieldDoneEditing:(id)sender {
  [sender resignFirstResponer];
}

and I'm mixed up in wiring the .xib part.

我在连接 .xib 部分时搞混了。

回答by skonb

Make the controller a delegate of the UITextField/UITextView in IB or from code like textField.delegate = self;

使控制器成为 IB 中 UITextField/UITextView 的委托,或者来自类似的代码 textField.delegate = self;

Editted: For this you need to declare the controller a delegate of UITextFieldDelegate/UITextViewDelegate as

编辑:为此,您需要将控制器声明为 UITextFieldDelegate/UITextViewDelegate 的委托为

@interface Controller : <UITextFieldDelegate> { ...

, then override the method:

,然后覆盖该方法:

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

for UITextField and

对于 UITextField 和

-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
    [textView resignFirstResponder];
    return YES;
}

for UITextView

用于 UITextView

回答by Josh Sherick

In your .xib, right click on your text view, drag to "File's Owner", and click "delegate". Should work now?

在您的 .xib 中,右键单击文本视图,拖动到“文件所有者”,然后单击“委托”。现在应该工作吗?

Edit: Whoops, sorry I'm an idiot, do what that other guy says. If you don't know how to set the delegate in code though, you can do it my way in IB.

编辑:哎呀,对不起,我是个白痴,照别人说的做。如果你不知道如何在代码中设置委托,你可以在 IB 中按照我的方式进行。

回答by Luis

Let me make my first contribution: If you have multiple text fields, group them in a @property (strong, nonatomic)

让我做出我的第一个贡献:如果您有多个文本字段,请将它们分组在一个 @property(强,非原子)中

*.h

*。H

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *collectingData;

*.m

*.m

-(BOOL)textFieldShouldReturn:(UITextField *)boxes
    {
        for (UITextField *boxes in collectingData) {
        [boxes resignFirstResponder];
    }
    return YES;

}