iOS UITextView 点击键盘 Return 按钮触发事件 - Objective-C
要在 UITextView 上实现点击键盘的 Return 按钮时触发事件,您需要实现 UITextViewDelegate 协议,并为 UITextView 设置代理。
首先,在 UIViewController 中声明遵循 UITextViewDelegate 协议:
@interface YourViewController : UIViewController <UITextViewDelegate>
然后,设置 UITextView 的代理为当前的 UIViewController 实例:
yourTextView.delegate = self;
最后,实现 UITextViewDelegate 的 textViewShouldReturn: 方法,在该方法中处理点击 Return 按钮的逻辑:
- (BOOL)textViewShouldReturn:(UITextView *)textView {
[textView resignFirstResponder]; // 隐藏键盘
// 处理点击 Return 按钮的逻辑
return YES;
}
这样,当用户点击 Return 按钮时,键盘会隐藏,并且 textViewShouldReturn: 方法会被调用,您可以在该方法中执行您想要的操作。
原文地址: https://www.cveoy.top/t/topic/p5Wz 著作权归作者所有。请勿转载和采集!