1

I am subclassing UITextView, and i got it working, but i also want to do some extra work when the UITextView Delegate methods are called. here is what i have so far.

ThanaaTextView.h

#import <UIKit/UIKit.h>

#import "ThaanaDelegate.h"


@interface ThaanaTextView : UITextView  {

    @private
    ThaanaDelegate * _thaanaDelegate;

}

ThanaaTextView.m

#import "ThaanaTextView.h"


@implementation ThaanaTextView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
       //do some extra stuff to textview
       //set delegate
       self.delegate = _thaanaDelegate;



    }
    return self;
}


}

@end

and here is my delegate class

ThaanaDelegate.h

 #import <UIKit/UIKit.h>
 #import <Foundation/Foundation.h>


@interface ThaanaDelegate : NSObject <UITextViewDelegate> {

    NSMutableArray* _lines;
}

   +(NSString*) reverseText:(NSString*) text withFont:(UIFont*) font carretPosition:(NSRange*) cpos Lines:(NSMutableArray*) lines Bounds:(CGRect) bounds;
-(void) setText:(NSString*) txt textview:(UITextView *)textView;


@end

ThaanaDelegate.m

 -(BOOL) textView:(UITextView*) textView shouldChangeTextInRange:(NSRange) range replacementText:(NSString*) text {
    //delegate method i want to do things in

    return NO;
}



-(void) setText:(NSString*) txt textview:(UITextView *)textView {

    //stuff
    return txt;
}

it compiles and runs without errors. but the delegate functions are never called. what am I'm missing.

Jinah Adam
  • 1,125
  • 2
  • 14
  • 27

1 Answers1

3

When you init the ThanaTextView, the delegate is not yet set.

self.delegate = _thaanaDelegate;

_thaanaDelegate is nil at this point. Ergo, you are setting it to nil.

Edit:

ThaanaTextView *ttv = [[ThaanaTextView alloc] initWithFrame:textViewFrame];
ttv.delegate = someDelegateHere;
CrimsonDiego
  • 3,616
  • 1
  • 23
  • 26
  • You can set it AFTER your init method (not in your method but before calling it). Or using a custom init method like initWithDelegate ... – user1226119 Jun 13 '12 at 20:56
  • You'd have to set it after the object is created. @user1226119 setting properties after alloc but before init is a very bad idea. – CrimsonDiego Jun 13 '12 at 20:57
  • @CrimsonDiego, omg i was tired when i wrote that. Clearly you have to set it AFTER alloc. – user1226119 Jun 13 '12 at 21:01
  • I know that. I was stating that setting it **after** alloc but before **init** is a bad idea, which it is as it goes against Cocoa conventions. Being tired is not an excuse for snapping at someone. Please read more carefully next time. – CrimsonDiego Jun 13 '12 at 21:02