【问题标题】:UITextInput protocol usage for UITextField and UITextView to manage selection results in crashUITextField 和 UITextView 的 UITextInput 协议用于管理崩溃中的选择结果
【发布时间】:2011-12-16 22:37:49
【问题描述】:

UITextField 和 UITextView 都采用 UITextInput 协议。 UITextView 的 selectedRange 属性返回 NSRange,其中 UITextField 没有任何选择属性/方法。我想使用一个例程来管理 UITextField 或 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 属性上出现“无法识别的选择器发送到实例”时才会立即崩溃。

我做错了什么?

[编辑:似乎在 iOS 5 中工作,但在 iOS 4 中的设备上崩溃。这是 iOS 5 的变化吗?文档说协议是 3.2+。]

【问题讨论】:

  • 哎呀。知道如何解决这个问题吗? @jasongregori

标签: ios uitextfield uitextview uitextinput


【解决方案1】:

我在自己的应用程序中验证了 UITextView 和 UITextField 在 iOS 4 模拟器中都以 -[UITextView selectedTextRange]: unrecognized selector 崩溃,但在 iOS 5 模拟器中没有。

这个问题的答案表明与 UITextInput 的一致性是 iOS 5 中的新事物:Can I select a specific block of text in a UITextField?。所以看起来协议本身是 iOS 3.2,但 UITextView/Field 在 iOS 5 之前并没有使用该协议。

【讨论】:

    【解决方案2】:

    @Absinthe 的编辑是正确的。 UITextField 直到 iOS 5 才开始遵循 UITextInput 协议。

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      相关资源
      最近更新 更多