繁体   English   中英

如何将完成按钮添加到键盘?

[英]How to add Done button to the keyboard?

更新:

我还尝试实现 UITextViewDelegate 委托,然后在我的 controller 中执行:

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

我还将文本视图的委托设置为自身(控制器视图实例)。

单击“完成”按钮仍会插入一个新行:(


更新:

到目前为止我做了什么。 我已经通过我的视图 controller 实现了一个 UITextFieldDelegate。

我已通过插座将文本视图连接到视图 controller。

然后我做了:


self.myTextView.delegate = self;

和:

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

但是当我点击完成按钮时,它只是添加了一个新行。

所以我的场景中有一个 UITextView 元素,当用户点击它时,键盘会出现并且可以对其进行编辑。

但是,我不能关闭键盘。

如何将“完成”按钮添加到键盘以便将其关闭?

那很简单:)

[textField setReturnKeyType:UIReturnKeyDone];

要关闭键盘,请在 class 中执行<UITextFieldDelegate>协议,设置

textfield.delegate = self;

并使用

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
}

要么

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

Go 到你的 storyboard,select 你的文本字段,在属性检查器下有一个选项,上面写着“返回键”...select“完成”。

然后 go 进入你的 ViewController 并添加:

- (IBAction)dismissKeyboard:(id)sender;
{
    [textField becomeFirstResponder];
    [textField resignFirstResponder];
}

然后 go 回到您的文本字段,单击 outlets 并链接“Did end on exit”以取消键盘操作。

添加 UIToolBar 作为自定义视图,其中将包含完成按钮作为 UIBarButtonItem。

这是将“完成”按钮添加到任何类型的键盘的更安全、更简洁的方法。 创建 UIToolBar 添加 Done Button 并设置任何 UITextField 或 UITextView 的 inputAccessoryView。

UIToolbar *ViewForDoneButtonOnKeyboard = [[UIToolbar alloc] init];
[ViewForDoneButtonOnKeyboard sizeToFit];
UIBarButtonItem *btnDoneOnKeyboard = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleBordered target:self
                                                              action:@selector(doneBtnFromKeyboardClicked:)];
[ViewForDoneButtonOnKeyboard setItems:[NSArray arrayWithObjects:btnDoneOnKeyboard, nil]];

myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard;

完成按钮的 IBAction

 - (IBAction)doneBtnFromKeyboardClicked:(id)sender
  {
      NSLog(@"Done Button Clicked.");

      //Hide Keyboard by endEditing or Anything you want.
      [self.view endEditing:YES];
  }

SWIFT 3

var ViewForDoneButtonOnKeyboard = UIToolbar()
ViewForDoneButtonOnKeyboard.sizeToFit()
var btnDoneOnKeyboard = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: #selector(self.doneBtnFromKeyboardClicked))
ViewForDoneButtonOnKeyboard.items = [btnDoneOnKeyboard]
myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard

Function

  @IBAction func doneBtnFromKeyboardClicked (sender: Any) {
     print("Done Button Clicked.")
    //Hide Keyboard by endEditing or Anything you want.
    self.view.endEditing(true)
  }

Swift版本:

在你的视图控制器中:

textField.returnKeyType = UIReturnKeyType.Done
textField.delegate = self

在你的 ViewController 之后

extension MyViewController: UITextFieldDelegate {        
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}

在文本字段的检查器中的text field选项下设置'Return Key'选项。 然后右键单击文本字段并按住CTRL select 'Did End On Exit'并将其拖到view controllers文件中。 这将创建一个IBAction方法。 为该方法命名,然后输入以下值:

[textField becomeFirstResponder];
[textField resignFirstResponder];

您的方法应如下所示:

- (IBAction)methodName:(id)sender;
{
    [sender becomeFirstResponder];
    [sender resignFirstResponder];
}

好的,所以我也一直在为这个问题而苦苦挣扎。 目前我正在访问 UITableViewCell 中的 UITextView(通过标签)。 因为我使用的是原型单元格,所以我不能像每个人建议的那样使用 IBActions 或 IBOutlets。 相反,我正在使用;

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

每次用户点击按钮时,这都会为我提供文本。 我当时的解决方案是获取新行的 ascii 字符; “/n”。 如果那是输入的文本,我会辞去第一响应者的职务。 例如;

 // If the text length is 0 then the user is deleting something, so only check the ascii character if there is text to check if (text.length:= 0) { // Get the Ascii character int asciiCode = [text characterAtIndex;0], // If the ascii code is /n or new line; then resign first responder if (asciiCode == 10) { [alertTextView resignFirstResponder]; [DeviceTextView resignFirstResponder]; } }

不确定是否还有其他人需要这个 hacky 解决方案,但我想我会把它放在那里以防万一有人需要它!

这是在键盘上添加完成按钮的最佳方式

textField.returnKeyType = UIReturnKeyDone;

以下是我的做法,在Swift 3 单击doneBtn时,让它发送.editingDidEndOnExit事件。 我使用此事件来处理多个文本字段之间的焦点问题。

// My customized UITextField
class MyTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

        // Add toolBar when keyboardType in this set. 
        let set : [UIKeyboardType] = [.numberPad, .phonePad]
        if (set.contains(self.keyboardType) ) {
            self.addDoneToolbar()
        }
    }

    // Add a toolbar with a `Done` button
    func addDoneToolbar() {

        let toolbar = UIToolbar()
        let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(onToolBarDone))

        toolbar.items = [space, doneBtn]
        toolbar.sizeToFit()

        self.inputAccessoryView = toolbar
    }

    @objc func onToolBarDone() {
        // I use `editingDidEndOnExit` to simulate the `Done` behavior 
        // on the original keyboard.
        self.sendActions(for: .editingDidEndOnExit)
    }
}

在 textView 的属性上的 Interface Builder 中,您可以将返回按钮类型设置为完成。

然后你需要检查何时按下返回按钮使用

  - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

检查文本是否 == "/n",然后通过辞去 textView 上的第一响应者来关闭键盘。

暂无
暂无

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

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