iphone - 如何退回 iPhone 键盘

标签 iphone objective-c ios xcode keyboard

分镜场景是这样的:

UITableViewController 带有 UITableView 和自定义原型(prototype) UITableViewCell:AddInfoCell

我在 AddInfoCell 中放置了一个可编辑的 UITextView

为了返回键盘,我将以下代码放在 UIViewController 类中,并使用选项 DidEndOnExit 将其连接到 UITextView:

- (IBAction)textFieldReturn:(id)sender {
    [sender resignFirstResponder];
}

但是我现在找不到适合这段代码的地方,因为 UITextViewAddInfoCell 类中。

它不能与 TableViewController 类或 AddInfoCell 类的 UITextView 连接。

我还尝试为 UITextView 创建一个 TextViewController 类,并将 textFieldReturn 代码放在那里,但我仍然无法将它连接到 Storyboard 中..

那么对于在这种情况下如何归还键盘有什么建议吗?

这是创建 AddInfoCell 的地方:

- (AddInfoCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"AddInfoCell";

AddInfoCell *addInfoCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (addInfoCell == nil) {
    addInfoCell = [[AddInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
return addInfoCell;

}

最佳答案

使用 UITextViewDelegate 委托(delegate)。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}

同时添加这个方法:

 -(BOOL)textViewShouldReturn:(UITextView *)textView {
   [textView resignFirstResponder];
   return YES;
  }

关于iphone - 如何退回 iPhone 键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12499046/

相关文章:

ios - 在 MKMapView 中渲染 map 线我的 map 被卡住

ios - 自定义一个 UITableViewCell 高亮样式

ios - AWS Cognito 数据集同步限制 - 每组同步超过 1mb 时会发生什么

iphone - 如何在 iOS 上动画后丢弃图像位置

objective-c - UITableView 滑动删除在 iOS8 上无法正常工作

ios - 没有自动布局的 iphone 6 加载 Storyboard 的条件

objective-c - 以编程方式创建 View

ios - Crashlytics 等工具如何工作?他们如何获得/制作崩溃日志?

ios - 我如何从 Graph api 资源管理器中的 apprequests 访问消息?

ios - native Controller 的 preferredStatusBarStyle