3

今天,在开发Reveal时,我意识到 iOS 7 的UITextView处理UITextInputTraits协议的一个错误。

该协议有一些方法可以设置当用户点击 a和其他类似的东西UITextInputTraits时应该显示什么样的键盘。UITextView

iOS 7 还引入了一个新selectable属性 onUITextView用于控制是否启用文本选择。这比以前版本的 iOS 所需的方法要好得多,在以前的版本中,您必须继承 UITextView 并返回 no canBecomeFirstResponder或设置一个inputDelegate并处理适当的委托回调以停止选择发生。

不幸的是,在 iOS 7 中同时设置editable和设置selectable为 NO 会破坏所有 UITextInputTraits方法。如果您尝试调用其中任何一个,您的应用程序将因instance does not respond to selector异常而崩溃。这很奇怪,因为 如果您调用协议中声明的任何方法,UITextView实例将返回 YES 。respondsToSelector:UITextInputTraits

在我看来,这似乎是 iOS 7 中的一个错误。我已将其报告给 Apple 作为radar://15063164。

我还注意到,对于通过代码(而不是在 Storyboards 中)创建的 UITextView,这个 bug 没有表达出来。我还不确定为什么。initWithCoder:可能有其他属性在起作用,或者 UITextView 是通过而不是 初始化的事实,initWithFrame:并且因此可能处于不同的状态。

一些显示错误的代码:

@interface IBAViewController ()

@property (strong, nonatomic) IBOutlet UITextView *editableAndSelectable;
@property (strong, nonatomic) IBOutlet UITextView *selectable;
@property (strong, nonatomic) IBOutlet UITextView *notEditableOrSelectable;

@end

@implementation IBAViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    /* 
     Remove the define below and we won't crash on the last NSAssert below.  Leave it in
     and we crash (assuming we've created and connected three UITextView's to the IBOutlets above
     in a storyboard for this view controller).
    */

#define USING_STORYBOARD

#ifndef USING_STORYBOARD
    self.editableAndSelectable = [[UITextView alloc] initWithFrame:self.view.bounds];
    self.selectable = [[UITextView alloc] initWithFrame:self.view.bounds];
    self.notEditableOrSelectable = [[UITextView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:self.editableAndSelectable];
    [self.view addSubview:self.selectable];
    [self.view addSubview:self.notEditableOrSelectable];
#endif

    self.editableAndSelectable.text = @"Text1";
    self.editableAndSelectable.editable = YES;
    self.editableAndSelectable.selectable = YES;

    self.selectable.text = @"Text2";
    self.selectable.editable = NO;
    self.selectable.selectable = YES;

    self.notEditableOrSelectable.text = @"Text3";
    self.notEditableOrSelectable.editable = NO;
    self.notEditableOrSelectable.selectable = NO;

    NSAssert(self.editableAndSelectable.editable == YES, @"Huh?");
    NSAssert(self.editableAndSelectable.selectable == YES, @"Huh?");

    NSAssert([self.editableAndSelectable respondsToSelector:@selector(isSecureTextEntry)], @"Huh?");
    NSAssert([self.editableAndSelectable isSecureTextEntry] == NO, @"Huh?");

    NSAssert(self.selectable.editable == NO, @"Huh?");
    NSAssert(self.selectable.selectable == YES, @"Huh?");

    NSAssert([self.editableAndSelectable respondsToSelector:@selector(isSecureTextEntry)], @"Huh?");
    NSAssert([self.editableAndSelectable isSecureTextEntry] == NO, @"Huh?");

    NSAssert(self.notEditableOrSelectable.editable == NO, @"Huh?");
    NSAssert(self.notEditableOrSelectable.selectable == NO, @"Huh?");

    NSAssert([self.notEditableOrSelectable respondsToSelector:@selector(isSecureTextEntry)], @"Huh?");
    NSAssert([self.notEditableOrSelectable isSecureTextEntry] == NO, @"Huh?"); // crashes here (on iOS 7)
}

@end

有没有其他人看过这个?或者有什么想法?

4

0 回答 0