0

我有一个像这样定义的子类

@protocol UMTextViewDelegate;

@interface UMTexView : UITextView <UITextViewDelegate> {

}

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

@end

@protocol UMTextViewDelegate <NSObject>
@optional
- (void)textViewDidSelectWordToDefine:(UMTexView*)textView;
@end

但我收到警告Property type 'id<UMTextViewDelegate>' is incompatible with type 'id<UITextViewDelegate>' inherited from 'UITextView'

如何抑制此警告?我试着添加这个:

@protocol UMTextViewDelegate <NSObject, UITextViewDelegate>

但没有运气。!!

编辑:

我没有使用 ARC

4

2 回答 2

0

您遇到的问题是关于前向声明。在您声明 的地方,编译delegate器不知道. 它唯一知道的是协议。UMTextViewDelegateUITextViewDelegateUMTextViewDelegate

你必须为类创建一个前向声明@class UMTexView;,然后放置协议声明,然后是类声明。

On a separate note, it's obvious UMTexView is supposed to be the text delegate for itself. Maybe it would be easier to have UMTexView descending directly from UIView and put an UITextView inside it. Then you wouldn't have any problem with delegate collisions and the UITextViewDelegate would be unaccessible externally.

于 2013-03-31T00:49:51.290 回答
0

Using @Sulthan's answer, this is what I came up with and it squished the warning.

@class UMTextView;

@protocol UMTextViewDelegate <NSObject, UITextViewDelegate>
@optional
- (void)textViewDidSelectWordToDefine:(UMTextView*)textView;
@end

@interface UMTextView : UITextView <UITextViewDelegate>

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

@end

The forward declaration of UMTextView followed by protocol declaration tells UMTextView that UMTextViewDelegate is indeed descends from UITextViewDelegate. That way I don't have to the route of adding a uiview and then adding a uitextview inside that view.

于 2013-03-31T20:37:43.443 回答