6

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?

Monolo
  • 18,205
  • 17
  • 69
  • 103
user500
  • 4,519
  • 6
  • 43
  • 56

1 Answers1

4

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

Stunner
  • 12,025
  • 12
  • 86
  • 145
George
  • 4,029
  • 1
  • 23
  • 31
  • 1
    I had tried to set this editable property but it also has `inputAccessoryView` so when it becomes first responder this `inputAccessoryView` shows. I just want to take control of becoming first responder on this object. Anyway, you are correct with your answer. That thing with uninformed pre-compiler is shame... – user500 Jun 17 '12 at 19:11