我的单元格中有 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 成为第一响应者呢?为什么我会收到此警告?有没有比我更好的解决方案?
最佳答案
我不确定,但我怀疑这个警告是因为预编译器知道 TextViewDelegate
但它还不知道这个协议(protocol)继承了 UITextView
协议(protocol).就像这样在上面声明它:
@class TextView;
@protocol TextViewDelegate <UITextViewDelegate>
- (BOOL)canBecomeFirstResponder:(TextView *)textView;
@end
@interface TextView : UITextView
@property (nonatomic, assign) id<TextViewDelegate> delegate;
@end
但我不确定我是否理解这个问题。
你有一个表格,在一个/多个/每个单元格中你有一个 UITextView
,对吗?
你想要 TextView 是可编辑的吗?
因为你可以简单地设置 [textView setEditable:FALSE];
希望这对您有所帮助。
问候,
乔治
关于Objective-c 用它的后代类型覆盖委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10995873/