1

I have a UITextView subclass. UITextView class has some delegate protocols like

- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;

that I would like to use as they were from my custom class. In other words, if I use MyCustomTextViewClass from a class (lets call it classX), I have to do this and set the delegate:

MyCustomTextViewClass *box = [[MyCustomTextViewClass alloc] initWithFrame:
                                   CGRectMake(111.0f, 123.0f, 190.0f, 50.0f)];
// ... bla bla.. set other parameters
[box setDelegate:self];

but in order to set the delegate, I have to declare classX using

<MyCustomTextViewClassDelegate>

and to do that, I have to add UITextView's delegate protocols to MyCustomTextViewClass.

How do I do that correctly?

simply do this on MyCustomTextViewClass?

@protocol MyCustomTextViewClassDelegate <NSObject>
@optional
- (void)textViewDidChange:(MyCustomTextViewClass *)textView;
- (void)textViewDidBeginEditing:(MyCustomTextViewClass *)textView;
@end

??? I don't see how this could forward the delegate protocols from the UITextView...

thanks for any help.

2 Answers 2

0

EDIT: New answer. Please understand that a UITextView subclass does not need to declare its own delegate protocol. It works like this: not all rectangles are squares, but all squares are rectangles. Your MyCustomTextView will behave and respond to any messages that a UITextView responds to, and it will also send messages to its delegate just like a UITextView would. If you wanted to add additional delegate methods, that are not covered by UITextView, then you would create a brand new protocol.

Anyway, think about why you need to subclass UITextView. Are you adding a subview to it, like a character counting label? It would be useful to subclass, in that case. If you're just creating a UITextView and changing some of the exposed properties, a custom subclass may be overkill. The reason I ask is because this question makes it seem that you don't quite understand object inheritance. Not trying to be a jerk or anything here, I want to help you.

Anyway, to the code:

//===================================
//ClassX.h
//===================================
#import "MyCustomTextView.h"

@interface ClassX : UIViewController <UITextViewDelegate>

-(void)loadTextView;

@property (nonatomic, strong) MyCustomTextView *customTextView;

@end
//===================================
//ClassX.m
//===================================
#import "ClassX.h"

@implementation ClassX
@synthesize customTextView;

-(void)loadTextView {
    if (!customTextView) {
        self.customTextView = [[MyCustomTextView alloc] initWithFrame:CGRectZero];
    customTextView.delegate = self;
    }
    customTextView.publicString = @"Public String Property!";    
}

#pragma mark UITextViewDelegate

-(void)textViewDidChange:(UITextView *)textView {
    if (textView == customTextView) {
        //THIS IS CALLED A CAST :
        MyCustomTextView *castedVariable = (MyCustomTextView *)textView;
        //now the object castedVariable will be treated as a MyCustomTextView object
        //so you can access methods and variables declared by the class
        textView.publicString = @"Hello World!"; //XCode raises an error, UITextView doesn't have this property
        castedVariable.publicString = @"Hello World!"; //this works fine
        //handle textViewDidChange method
        //you are now interacting with self.customTextView
    } else {
        //is your ClassX also in charge of 
        //a different UITextView? Handle here
    }

}
11
  • OK, but when they fire they will send out a (UITextView *) in, for example, "-(void)textViewDidChange:(UitextView *)textView". Wasn't more logical if they send a (MyCustomTextViewClass *) instead? Like in "-(void)textViewDidChange:(MyCustomTextViewClass *)textView"... this is my problem.
    – Duck
    Sep 7, 2012 at 2:49
  • If MyCustomTextView is a subclass of UITextView, it contains all the functionality of UITextView and behaves accordingly. What is the purpose of your subclass? What are you adding to it that the standard class doesn't provide? You may be doing some overkill or not understand what you're trying to accomplish
    – Daddy
    Sep 7, 2012 at 2:54
  • I think I cannot declare "-(void)textViewDidChange:(UitextView *)textView {"... instead I have to declare "-(void)textViewDidChange:(MyCustomTextViewClass *)textView {"... think about it. I am inside MyCustomTextViewClass. How can a MyCustomTextViewClass receive a UITextView object?
    – Duck
    Sep 7, 2012 at 2:54
  • I am not declaring any new protocol. I just want the protocols from UITextView to be forward using (MyCustomTextViewClass *) instead of (UITextView *) in all delegate protocols. When I use this class on another point of the application and set the delegate there, I need the delegates to fire sending that class MyCustomTextViewClass objects, not UITextView objects.
    – Duck
    Sep 7, 2012 at 2:56
  • You can cast the incoming UITextView as a MyCustomTextView. Try : MyCustomTextView *view = (MyCustomTextView *)textView; but I ask again, what is your MyCustomTextView? What is its purpose, how is it different? How does it differ from UITextView that you can't achieve by just declaring a UItextView and modifying it's exposed properties?
    – Daddy
    Sep 7, 2012 at 3:00
0

Your question is a little unclear. If you just want MyCustomTextViewClass to implement the UITextViewDelegate methods, then just declare it to do so:

@class MyCustomTextViewClass : UITextView <UITextViewDelegate> {
...
}

If you have a new protocol you're creating, and you have a class that implements both your new protocol and UITextViewDelegate, you can declare it to implement both like this:

@class MyDelegateClass : NSObject <UITextViewDelegate, MyCustomTextViewClassDelegate> {
...
}

If you want MyCustomTextViewClassDelegate to require all the same methods as UITextViewDelegate, then you declare it as inheriting from that protocol, like this:

@protocol MyCustomTextViewClassDelegate <UITextViewDelegate> {
...
}
2
  • I want what you said in the last part of your answer. My problem is what I put on the @protocol part of MyCustomTextViewClassDelegate? something like I put on the last part of my question?
    – Duck
    Sep 7, 2012 at 2:05
  • You can put whatever methods you want to extend UITextViewDelegate in the @protocol part. If you don't have any methods that extend UITextViewDelegate, then you don't need a new protocol – you can just use the first one above. Sep 7, 2012 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.