【发布时间】:2014-01-13 17:41:22
【问题描述】:
我目前正在尝试创建一个类似于 SMS 的屏幕,用户可以在其中编写一些文本并将其发送给其他用户。一切都按预期进行,直到我尝试清除我的文本视图并遇到崩溃。 我一直试图找到解决这个问题的方法,但我只是无法在网上找到足够的文档。就是这样,希望你们中的一个人知道解决此问题的方法。
实现
我的 UITextView 是 Peter Steinberger's implementation for iOS7 的子类,我将它与自定义 NSTextStorage 一起使用,如 objc.io 的“了解 TextKit”中所示,尤其是 source code,以便在消息中突出显示用户名。
在我的 ViewController 中,我像这样配置我的文本存储:
self.textStorage = [[[MyCustomTextStorage alloc] init] autorelease];
[self.textStorage addLayoutManager:self.textView.layoutManager];
然后在我的TextView的委托方法中:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
我将输入存储在我的自定义 TextStorage 中:
[self.textStorage.string stringByReplacingCharactersInRange:range withString:text];
崩溃
我可以通过self.textStorage.string 检索我的文本,然后通过替换输入字符串范围内的字符来清除我的文本视图。这工作得很好,但是当我尝试再次将我的 TextView 设置为第一响应者时,应用程序崩溃了。
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSCFString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {5, 0} out of bounds; string length 0'
提到的范围是我之前清除的字符串的范围,所以看起来我可以清除显示,但我的 TextStorage/TextView 保留对第一个编辑范围/字符串的引用。
对可能导致此崩溃的原因以及如何解决它有任何想法吗? 感谢您的关注;在这一点上,任何帮助表示赞赏,所以如果您有任何建议,请随时发布。 :)
【问题讨论】:
标签: ios objective-c uitextview textkit nstextstorage