gpt4 book ai didi

ios - 在 UITextView 上打字时按下返回按钮时有没有办法触发函数?

转载 作者:行者123 更新时间:2023-11-30 11:39:48 26 4
gpt4 key购买 nike

当我使用 UITextField 而不是 UITextView 时,我可以将协议(protocol) UITextFieldDelegate 添加到工作类并符合该协议(protocol)并添加类似功能:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
handleSend()
return true
}

按下键盘上的返回按钮会触发函数handleSend()

有一个名为 UITextViewDelegate 的协议(protocol),但没有名为 textViewShouldReturn 的函数。

如何使用 UITextView 实现此目的?

最佳答案

你可以尝试:

swift 4:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
handleSend()
}
return true
}

swift 3:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
handleSend()
}
return true
}

关于ios - 在 UITextView 上打字时按下返回按钮时有没有办法触发函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49374388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com