解决下述问题:我的问题已通过 Mavericks/Xcode 5.0.1 更新得到解决。看来这是一个 SDK 错误。
我在终端应用程序中遇到了看起来完全相同的问题 - 当我点击空格时(例如,阅读远程站点上显示的文本more
)我遇到了崩溃,因为文本字段没有空格字符,因此范围是出界。(有关发生这种情况的代码,请参见下文)
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
return NO;
}
这只是保持 textview 集中并处理所有按下的键。这是我一个接一个地抓取键盘字符以通过 telnet 客户端发送的方式。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"—%@— is replacementText", text);
return YES;
}
xterminal[37918:c07] — — is replacementText
一旦空间被挖掘,我就会看到。当我发现问题时我没有这个 - 添加它是为了确保 UITextView 看到了一个空间。
- (void)textViewDidChange:(UITextView *)textView {
// determine whether the key was a backspace
if([textView.text isEqualToString:@""]) {
uint8_t bsValue = kTelnetCharBS;
[connection sendData:[NSData dataWithBytes:&bsValue length:1]];
} else {
NSLog(@"—%@— in output buffer", textView.text);
// Not a backspace, send all but the 'X'
NSRange allButX = NSMakeRange(1, 1);
[connection sendString:[textView.text substringWithRange:allButX]];
}
textView.text = @"X";
}
我懂了xterminal[37918:c07] —X— in output buffer
紧接着,然后在
[connection sendString:[textView.text substringWithRange:allButX]];
该空间不存在或NSRange allButX = NSMakeRange(1, 1);
与其他可打印字符一样可以使用。
没有实现 UITextView 的其他委托方法。它是用
inputTextView = [[UITextView alloc] initWithFrame:CGRectZero];
紧接着
inputTextView.text = @"X";
inputTextView.delegate = self;
inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[self.view addSubview:inputTextView];
[inputTextView becomeFirstResponder];
我的空间在哪里?
更新:仍然不知道为什么 UITextView 的行为如此。UITextField 以我想要的方式工作,以相同的方式创建。要替换 UITextView 委托方法textViewDidChange:
,我正在使用这个:
[inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
否则相同的代码。