1

我是 UITextView 的子类,并且我得到了它的工作,但我还想在调用 UITextView 委托方法时做一些额外的工作。这是我到目前为止所拥有的。

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

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

4

1 回答 1

3

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

self.delegate = _thaanaDelegate;

_thaanaDelegate 此时为零。因此,您将其设置为零。

编辑:

ThaanaTextView *ttv = [[ThaanaTextView alloc] initWithFrame:textViewFrame];
ttv.delegate = someDelegateHere;
于 2012-06-13T20:49:12.873 回答