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

use*_*500 5 cocoa-touch delegates objective-c first-responder ios

我在我的单元格中有textView,有时在tableView滚动期间会发生一些奇怪的调用.系统使我的textView第一响应者.我发现这些调用做了不需要的行为:

#0 -[UITextView canBecomeFirstResponder] ()
#1 -[UIView(Hierarchy) deferredBecomeFirstResponder] ()
#2 -[UIView(Hierarchy) _promoteDescendantToFirstResponderIfNecessary] ()
Run Code Online (Sandbox Code Playgroud)

我无法找出为什么这些被调用,所以我试图通过扩展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
Run Code Online (Sandbox Code Playgroud)

而.m:

#import "TextView.h"

@implementation TextView

@synthesize delegate;

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

@end
Run Code Online (Sandbox Code Playgroud)

这个解决方案有效,但@property (nonatomic, assign) id<TextViewDelegate> delegate;我已经收到警告,我不知道为什么.它说Property type 'id<TextViewDelegate>' is incompatible with type 'id<UITextViewDelegate>' inherited from 'UITextView'.

那么,为什么系统想让textView第一响应者,如果我不这样做?为什么我收到这个警告?有没有比我更好的解决方案?

Geo*_*rge 4

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

@class TextView;

@protocol TextViewDelegate <UITextViewDelegate>

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

@end

@interface TextView : UITextView

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

@end
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助。

问候,

乔治