Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c overriding delegate with its descendant type

I have textView in my cell and sometimes during tableView scroll some weird calls happen. System make my textView first responder. I've found these calls do unwanted behavior:

#0 -[UITextView canBecomeFirstResponder] ()
#1 -[UIView(Hierarchy) deferredBecomeFirstResponder] ()
#2 -[UIView(Hierarchy) _promoteDescendantToFirstResponderIfNecessary] ()

I can't find out why are these called, so I've tried to deal with this by extending UITextView and overriding - canBecomeFirstResponder.

Here is my .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

And .m:

#import "TextView.h"

@implementation TextView

@synthesize delegate;

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

@end

This solution works but on the line @property (nonatomic, assign) id<TextViewDelegate> delegate; I've got warning and I don't know why. It says Property type 'id<TextViewDelegate>' is incompatible with type 'id<UITextViewDelegate>' inherited from 'UITextView'.

So why system want to make textView first responder if I do not? Why I'm getting this warning? Is there better solution than mine?

like image 897
user500 Avatar asked Jun 12 '12 11:06

user500


1 Answers

I'm not sure , but I suspect the warning is because the pre-compiler knows about TextViewDelegate but it does not know yet that this protocol is inheriting UITextView protocol. Just declare it above like this:

@class TextView;

@protocol TextViewDelegate <UITextViewDelegate>

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

@end

@interface TextView : UITextView

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

@end

But I'm not sure I understand the question. You have a table and in one/more/each cell you have an UITextView, correct? Do you want the text view to be editable? Because you can simply set [textView setEditable:FALSE];

Hope this helps.

Regards,

George

like image 156
George Avatar answered Sep 21 '22 17:09

George