iphone - 当用户点击 iPhone 键盘上的“发送”按钮时,如何执行选择器?

标签 iphone objective-c cocoa-touch uitextview

我有一个 UITextView。当用户按下发送键时,我希望能够自动执行选择器。我怎样才能做到这一点?我知道 UITextField 有

- (BOOL)textFieldShouldReturn:(UITextField *)textField

最佳答案

试试这个:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 
 replacementText:(NSString *)text; {
  // Any new character added is passed in as the "text" parameter.
  if ([text isEqualToString:@"\n"]) {
    // If the Done button was pressed, resign the keyboard.
    [textView resignFirstResponder];
    // Return FALSE so that the final '\n' character doesn't get added.
    return NO;
  }
  // For any other character return TRUE so that the text gets added to the view.
  return YES;
}

希望有帮助!

关于iphone - 当用户点击 iPhone 键盘上的“发送”按钮时,如何执行选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7315542/

相关文章:

iphone - 点击时隐藏导航栏

ios - 如何将应用程序音量与系统音量(iOS设备音量物理键)分开设置?

ios - 以编程方式使用日期选择器更改按钮标题时,它始终会重置

iphone - tableView 作为半透明 View 中的 subview

ios - NSURLSession 下载任务-进度条问题

ios - ^ 在 objective c ios 中是什么意思?

cocoa - 如何以视觉格式设置自动布局默认间距指标的优先级?

ios - 收到两个或多个通知时只执行一次选择器

iphone - 适用于 iPhone 和 Android 的消息传递 API

objective-c - 如何丢弃与 UITableView 关联的所有表格单元格以刷新它们