分镜场景是这样的:
UITableViewController
带有 UITableView
和自定义原型(prototype) UITableViewCell
:AddInfoCell
。
我在 AddInfoCell
中放置了一个可编辑的 UITextView
。
为了返回键盘,我将以下代码放在 UIViewController
类中,并使用选项 DidEndOnExit
将其连接到 UITextView
:
- (IBAction)textFieldReturn:(id)sender {
[sender resignFirstResponder];
}
但是我现在找不到适合这段代码的地方,因为 UITextView
在 AddInfoCell
类中。
它不能与 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/