Custom Delegate fo UITextView



我正在子类化UITextView,我让它工作,但是当调用UITextView Delegate方法时,我也想做一些额外的工作。 这是我到目前为止所拥有的。

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

这是我的代表课

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;
}

它编译和运行没有错误。 但从不调用委托函数。 我错过了什么。

当您初始化 ThanaTextView 时,尚未设置委托。

self.delegate = _thaanaDelegate;

在这一点上,_thaanaDelegate为零。因此,您将其设置为零。

编辑:

ThaanaTextView *ttv = [[ThaanaTextView alloc] initWithFrame:textViewFrame];
ttv.delegate = someDelegateHere;

最新更新