【问题标题】:How to return the iPhone keyboard如何退回 iPhone 键盘
【发布时间】:2012-09-12 00:25:42
【问题描述】:

故事板场景是这样的:

UITableViewControllerUITableView 和自定义原型 UITableViewCell: AddInfoCell

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

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

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

但我现在找不到适合此代码的位置,因为 UITextViewAddInfoCell 类中。

无法与TableViewController 类或AddInfoCell 类中的UITextView 连接。

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

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

这是创建 `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;

}

【问题讨论】:

  • 将文本字段委托设置为您的视图控制器类。同样的方法也可以。
  • 你能详细解释一下吗?
  • 当您创建自定义单元格的对象时,例如。 AddInfoCell *cell,然后设置 cell.delegate = self.您可以在您的视图控制器本身中调用您的文本字段代表。
  • 它是在 cellForRowAtIndexPath 中创建的,但我不能在这里写 addInfoCell.delegate = self 当我开始写作时它没有作为一个选项出现。但是如果你可以显示它听起来比其他答案简单得多一个例子并将其描述为另一个答案?
  • 你不能为单元设置代表吗?我很抱歉我现在没有示例代码。也许我会把它送给你。

标签: iphone objective-c ios xcode keyboard


【解决方案1】:

使用UITextViewDelegate委托。

- (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;
  }

【讨论】:

  • 谢谢,但我应该在哪里添加此代码?到处都试过了,但没有任何区别。你能解释一下完成这项工作的过程吗?
  • 将这个添加到你的 UITextView 所在的位置。同时绑定它的委托。
  • 我想在 UITableViewCell 类中的含义。到目前为止非常感谢,但我需要问:如何绑定它的代表?
  • 如果在 xib 中的 UITextView 然后右键单击并且有你拖到文件所有者的委托。现在添加 .h 文件 @interface viewController :UIViewController
【解决方案2】:

addInfoCell.cellTextView.delegate = self;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2010-11-19
    相关资源
    最近更新 更多