UITextField和UITextView的UITextInput协议用法,用于管理崩溃中的选择结果 [英] UITextInput protocol usage for UITextField and UITextView to manage selection results in crash
问题描述
UITextField和UITextView均采用UITextInput协议. UITextView的selectedRange属性返回NSRange,其中UITextField没有任何选择属性/方法.我想使用一个例程来管理在UITextField或UITextView中的插入.
UITextField and UITextView both adopt the UITextInput protocol. UITextView's selectedRange property returns NSRange, where UITextField doesn't have any selection properties/methods. I'd like to use one routine to manage insertion in either UITextField or UITextView.
所以我执行以下操作:
id<UITextInput> textInput = nil;
if ([self.aTextView isFirstResponder]) {
textInput = self.aTextView;
} else if ([self.aTextField isFirstResponder]) {
textInput = self.aTextField;
}
if (textInput != nil) {
UITextRange * selectedRange = textInput.selectedTextRange;
// ...
}
仅当selectedTextRange属性上的无法识别的选择器发送到实例"时立即崩溃.
Only to promptly crash with 'unrecognized selector sent to instance' on the selectedTextRange property.
我在做什么错了?
推荐答案
我在自己的应用中验证了UITextView和UITextField都在iOS 4模拟器中以-[UITextView selectedTextRange]: unrecognized selector
崩溃,但在iOS 5模拟器中没有崩溃.
I verified in my own app that UITextView and UITextField both crash with -[UITextView selectedTextRange]: unrecognized selector
in the iOS 4 simulator but not the iOS 5 simulator.
此问题的答案表明,与UITextInput的一致性是iOS 5中的新事物:
The answer to this question suggests that the conformance to UITextInput is a new thing in iOS 5: Can I select a specific block of text in a UITextField? . So it appears that the protocol itself is iOS 3.2, but UITextView/Field didn't make use of that protocol before iOS 5.
这篇关于UITextField和UITextView的UITextInput协议用法,用于管理崩溃中的选择结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!