简体   繁体   English

用户按下UITextView的键盘完成按钮时如何执行操作

[英]How to perform an action when the user hits the keyboard's done button for a UITextView

I know there are lots of similar questions about this already, but after looking at many of them they are involving UITextField and consequently UITextFieldDelegate's textFieldShouldReturn method. 我知道已经有很多类似的问题,但是在研究了许多问题之后,它们都涉及UITextField,因此也涉及UITextFieldDelegate的textFieldShouldReturn方法。 However I have a UITextView, not a UITextField, and I want to know when the user has hit the done button on the associated keyboard. 但是,我有一个UITextView,而不是UITextField,我想知道用户何时单击了相关键盘上的完成按钮。

I have a table view where when the user taps on one of the rows the table enters an editing mode and the user can enter text into a UITextView which is within a cell. 我有一个表格视图,当用户点击其中一行时,表格进入编辑模式,并且用户可以将文本输入到单元格内的UITextView中。 Here is some code: 这是一些代码:

The cell containing the text view 包含文本视图的单元格

class ReportTextEntryCell : UITableViewCell
{
    @IBOutlet weak var commentsTextView: UITextView!
}

The creation of the cell, which is called from within cellForRowAt 单元的创建,从cellForRowAt内部调用

func getTextEntryCell() -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "TextEntryCellID") as? ReportTextEntryCell
    cell!.commentsTextView.delegate           = self
    cell!.commentsTextView.keyboardAppearance = .light
    cell!.commentsTextView.keyboardType       = UIKeyboardType.default
    cell!.commentsTextView.tintColor          = UIColor.black
    cell!.commentsTextView.returnKeyType      = .done
    return cell!
}

The keyboard appears, the user can type text. 出现键盘,用户可以键入文本。

The table view controller implements UITextViewDelegate and textViewShouldBeginEditing and shouldChangeTextIn are both invoked. 表视图控制器实现UITextViewDelegatetextViewShouldBeginEditingshouldChangeTextIn都调用。 But I was expecting textViewShouldEndEditing would be called when the user taps the Done button on the keyboard, but it is not. 但是我期望当用户点击键盘上的“完成”按钮时会调用textViewShouldEndEditing ,但事实并非如此。

How can I know when the user hits the done button? 我怎么知道用户何时点击完成按钮?

You need to inherit the UITextViewDelegate and set the delegate for your textView and then you can use the following: 你需要继承UITextViewDelegate并设置delegate为您textView ,然后你可以使用以下命令:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text == "\n" {
        // User pressed Done
        textView.resignFirstResponder()
        return false
    }
    return true
}

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

相关问题 如何在“完成”按钮键盘上添加动作? - How to add action to “done” button keyboard? 单击捕获按钮后,如何仅在iphone图像选择器中完成捕获时如何执行操作 - How to perform an action only when capture is done in iphone image-Picker when ever click the capture button UITextView完成按钮操作 - 返回键 - UITextView done button action - on return key 在用户在UITextView中输入内容之前,如何禁用“完成”按钮? - How to keep Done button disabled until user types something into the UITextView? 用户单击目标C中的myLocation按钮时如何执行操作? - How to perform action when user click on myLocation button in Objective C? “完成”按下时隐藏UITextView的虚拟键盘 - Hide Virtual Keyboard of UITextView when 'Done' Presses 在键盘外触摸时完成按钮动作 - Swift Xcode - Button action done when touched outside of keyboard - Swift Xcode 如果用户在返回键盘之前按下了按钮,如何将按钮的操作应用于文本字段 - How to apply the button's action to a text filed if the user pressed the button before returning the keyboard the keyboard 如何从Webview键盘获取完成按钮操作 - How to get Done button action from keyboard of Webview 当用户点击键盘上的返回键时如何单击UIButton - How to click UIButton when user hits return on keyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM