Ios 完全禁用';退格';被迅速逼迫

Ios 完全禁用';退格';被迅速逼迫,ios,swift,uitextview,uitextviewdelegate,Ios,Swift,Uitextview,Uitextviewdelegate,我正在开发一个应用程序,用户需要能够键入“转发”,但不能删除任何已键入的内容(例如,返回并编辑) 有几个答案大致符合我的要求: -这是Swift的一个旧版本,我想,因为我在这之后出现了很多错误(不推荐使用,缺少参数等)。当我尝试这一点时,我想做如下的事情: func disableBackspace() { //do a check to see if the range of characters has reduced if range of characters in the strin

我正在开发一个应用程序,用户需要能够键入“转发”,但不能删除任何已键入的内容(例如,返回并编辑)

有几个答案大致符合我的要求:

-这是Swift的一个旧版本,我想,因为我在这之后出现了很多错误(不推荐使用,缺少参数等)。当我尝试这一点时,我想做如下的事情:

func disableBackspace() {

//do a check to see if the range of characters has reduced
if range of characters in the string reduces (<0?) {

//if it has then don't allow the 'character' to be typed
return false 
}
}
并通过执行以下操作使我的VC成为textView代理

override func viewDidLoad() {
    self.textViewOutlet.delegate = self

    super.viewDidLoad()
}
但那没用。它不打印任何内容,仍然允许用户退格


任何想法都非常感谢

您只需实现
textView(uu:shouldChangeTextIn:replacementText:)
并检查
是否为空
replacementText

空替换文本==按退格键

如果
文本
1返回false
。Else
返回true`

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text.isEmpty {
        return false
    }
    return true
}

您只需实现
textView(uu1;:shouldChangeTextIn:replacementText:)
并检查
是否为空
replacementText

空替换文本==按退格键

如果
文本
1返回false
。Else
返回true`

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text.isEmpty {
        return false
    }
    return true
}

使用以下代码:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let  char = text.cString(using: String.Encoding.utf8)!
    let isBackSpace = strcmp(char, "\\b")

    if (isBackSpace == -92) {
        return false
    }
    return true
}

使用以下代码:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let  char = text.cString(using: String.Encoding.utf8)!
    let isBackSpace = strcmp(char, "\\b")

    if (isBackSpace == -92) {
        return false
    }
    return true
}

这非常有效-关于如何检测Swift中按下的退格,有很多问题和答案-这是绝对英里的最佳答案-谢谢-说我必须再等5分钟!这非常有效-关于如何检测Swift中按下的退格,有很多问题和答案-这是绝对英里的最佳答案-谢谢-说我必须再等5分钟!你好-谢谢,我在另一个答案上看到了这个,但我不明白为什么它会等于92-你知道吗?strcmp()基本上是C语言函数,它返回两个字符之间的数字差。这里-92是“Backspace”和“\\b”字符之间的区别。您可以在这里参考更多详细信息:@nc14我已经测试过了,代码正在按照您的预期工作。您好-谢谢,我在另一个答案的某个地方看到了这一点,但我不明白为什么它会等于92-您知道吗?strcmp()基本上是C语言函数,它返回两个字符之间的数字差。这里-92是“Backspace”和“\\b”字符之间的区别。您可以在这里参考更多详细信息:@nc14我已经测试过,代码工作正常。