6

我的单元格中有 textView,有时在 tableView 滚动期间会发生一些奇怪的调用。系统让我的 textView 成为第一响应者。我发现这些调用会产生不良行为:

#0 -[UITextView canBecomeFirstResponder] ()
#1 -[UIView(Hierarchy) deferredBecomeFirstResponder] ()
#2 -[UIView(Hierarchy) _promoteDescendantToFirstResponderIfNecessary] ()

我不知道为什么要调用这些,所以我尝试通过扩展UITextView和覆盖来解决这个问题- canBecomeFirstResponder

这是我的.h:

#import <UIKit/UIKit.h>

@protocol TextViewDelegate;

@interface TextView : UITextView

@property (nonatomic, assign) id<TextViewDelegate> delegate;

@end

@protocol TextViewDelegate <UITextViewDelegate>

- (BOOL)canBecomeFirstResponder:(TextView *)textView;

@end

和.m:

#import "TextView.h"

@implementation TextView

@synthesize delegate;

- (BOOL)canBecomeFirstResponder
{
    return [self.delegate respondsToSelector:@selector(canBecomeFirstResponder:)] ? [self.delegate canBecomeFirstResponder:self] : NO;
}

@end

该解决方案有效,但在线上@property (nonatomic, assign) id<TextViewDelegate> delegate;我收到了警告,但我不知道为什么。它说Property type 'id<TextViewDelegate>' is incompatible with type 'id<UITextViewDelegate>' inherited from 'UITextView'

那么,如果我不这样做,为什么系统要让 textView 成为第一响应者呢?为什么我会收到此警告?有比我更好的解决方案吗?

4

1 回答 1

4

我不确定,但我怀疑这个警告是因为预编译器知道TextViewDelegate但它还不知道这个协议正在继承UITextView协议。只需像这样在上面声明它:

@class TextView;

@protocol TextViewDelegate <UITextViewDelegate>

- (BOOL)canBecomeFirstResponder:(TextView *)textView;

@end

@interface TextView : UITextView

@property (nonatomic, assign) id<TextViewDelegate> delegate;

@end

但我不确定我是否理解这个问题。您有一张桌子,并且在一个/多个/每个单元格中都有一个UITextView,对吗?您希望文本视图可编辑吗?因为你可以简单地设置[textView setEditable:FALSE];

希望这可以帮助。

问候,

乔治

于 2012-06-16T12:35:55.147 回答