【问题标题】:work around for dictation / custom text view bug解决听写/自定义文本视图错误
【发布时间】:2014-01-07 20:10:56
【问题描述】:

我有一个实现UIKeyInput 并覆盖shouldBecomeFirstResponder 以返回YES 的视图。点击此视图时,键盘会弹出。如果用户点击听写按钮,按下完成,然后尝试关闭键盘,应用程序崩溃:

Fatal Exception: NSInternalInconsistencyException NSInternalInconsistencyException
We were never set up properly to stream in this document.

我想知道是否有人知道解决此错误的方法?我尝试实现UITextInput 并将听写相关方法留空,这确实有效。但是,UITextInput 正在访问我的 insertText 方法,当用户尝试使用听写并放入垃圾邮件时。我还没有准备好实施听写,所以我现在只想通过某种解决方法来禁用它。任何意见将不胜感激!

【问题讨论】:

    标签: ios iphone cocoa-touch


    【解决方案1】:

    实际上,实现UITextInput 解决了这个问题。当用户成功听写时,它只会向您的 insertText 方法发送一个空格(不完全确定如何让听写在自定义文本视图中正常工作,现在我只想修复这个错误)。下面我列出了您必须为UITextInput 使用的所有方法和属性,以节省您一些时间:

    属性:

    @property(nonatomic, readonly) UITextPosition *beginningOfDocument;
    @property(nonatomic, readonly) UITextPosition *endOfDocument;
    @property(nonatomic, assign) id<UITextInputDelegate> inputDelegate;
    @property(nonatomic, readonly) UITextRange *markedTextRange;
    @property(nonatomic, copy) NSDictionary *markedTextStyle;
    @property(readwrite, copy) UITextRange *selectedTextRange;
    @property(nonatomic, readonly) id<UITextInputTokenizer> tokenizer;
    

    方法:

    - (UITextWritingDirection)baseWritingDirectionForPosition:(UITextPosition *)position inDirection:(UITextStorageDirection)direction
    {
        return nil;
    }
    
    - (CGRect)caretRectForPosition:(UITextPosition *)position
    {
        return CGRectZero;
    }
    
    - (void)unmarkText
    {
    
    }
    
    - (UITextRange *)characterRangeAtPoint:(CGPoint)point
    {
        return nil;
    }
    - (UITextRange *)characterRangeByExtendingPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction
    {
        return nil;
    }
    - (UITextPosition *)closestPositionToPoint:(CGPoint)point
    {
        return nil;
    }
    - (UITextPosition *)closestPositionToPoint:(CGPoint)point withinRange:(UITextRange *)range
    {
        return nil;
    }
    - (NSComparisonResult)comparePosition:(UITextPosition *)position toPosition:(UITextPosition *)other
    {
        return nil;
    }
    - (void)dictationRecognitionFailed
    {
    }
    - (void)dictationRecordingDidEnd
    {
    }
    - (CGRect)firstRectForRange:(UITextRange *)range
    {
        return CGRectZero;
    }
    
    - (CGRect)frameForDictationResultPlaceholder:(id)placeholder
    {
        return CGRectZero;
    }
    - (void)insertDictationResult:(NSArray *)dictationResult
    {
    }
    - (id)insertDictationResultPlaceholder
    {
        return nil;
    }
    
    - (NSInteger)offsetFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
    {
        return nil;
    }
    - (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset
    {
        return nil;
    }
    - (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset
    {
        return nil;
    }
    
    - (UITextPosition *)positionWithinRange:(UITextRange *)range farthestInDirection:(UITextLayoutDirection)direction
    {
        return nil;
    }
    - (void)removeDictationResultPlaceholder:(id)placeholder willInsertResult:(BOOL)willInsertResult
    {
    }
    - (void)replaceRange:(UITextRange *)range withText:(NSString *)text
    {
    }
    - (NSArray *)selectionRectsForRange:(UITextRange *)range
    {
        return nil;
    }
    - (void)setBaseWritingDirection:(UITextWritingDirection)writingDirection forRange:(UITextRange *)range
    {
    }
    - (void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
    {
    }
    
    - (NSString *)textInRange:(UITextRange *)range
    {
        return nil;
    }
    - (UITextRange *)textRangeFromPosition:(UITextPosition *)fromPosition toPosition:(UITextPosition *)toPosition
    {
        return nil;
    }
    

    【讨论】:

    • 谢谢。节省了很多时间。
    • @ZackMatthews 没问题,很高兴它有帮助:)
    • 我有一个 UISearchBar(所以,不是自定义视图)有这个问题。我实际上尝试子类化 UISearchBar 以在其上显式实现 UITextInput 协议,但我仍然遇到您上面提到的相同崩溃。肯定是有什么问题……
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    相关资源
    最近更新 更多