Objective-c用其后代类型覆盖委托

时间:2012-06-12 11:38:35

标签: objective-c ios cocoa-touch delegates first-responder

我的单元格中有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第一响应者,如果我不这样做?为什么我收到这个警告?有没有比我更好的解决方案?

1 个答案:

答案 0 :(得分: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];

即可

希望这有帮助。

此致

乔治