【发布时间】:2012-06-12 11:38:34
【问题描述】:
我的单元格中有 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 成为第一响应者?为什么我会收到此警告?有比我更好的解决方案吗?
【问题讨论】:
标签: objective-c ios cocoa-touch delegates first-responder