iPhone-转发父类的委托
我有一个UITextView子类。UITextView类有一些委托协议,如iPhone-转发父类的委托,iphone,ios,cocoa-touch,ipad,Iphone,Ios,Cocoa Touch,Ipad,我有一个UITextView子类。UITextView类有一些委托协议,如 - (void)textViewDidChange:(UITextView *)textView; - (void)textViewDidBeginEditing:(UITextView *)textView; 我想使用它们,因为它们来自我的自定义类。换句话说,如果我从一个名为classX的类中使用MyCustomTextViewClass,我必须这样做并设置委托: MyCustomTextViewClass *box
- (void)textViewDidChange:(UITextView *)textView;
- (void)textViewDidBeginEditing:(UITextView *)textView;
我想使用它们,因为它们来自我的自定义类。换句话说,如果我从一个名为classX的类中使用MyCustomTextViewClass,我必须这样做并设置委托:
MyCustomTextViewClass *box = [[MyCustomTextViewClass alloc] initWithFrame:
CGRectMake(111.0f, 123.0f, 190.0f, 50.0f)];
// ... bla bla.. set other parameters
[box setDelegate:self];
但是为了设置委托,我必须使用
<MyCustomTextViewClassDelegate>
为此,我必须将UITextView的委托协议添加到MyCustomTextViewClass中
我如何正确地做到这一点
只需在MyCustomTextViewClass上执行此操作
@protocol MyCustomTextViewClassDelegate <NSObject>
@optional
- (void)textViewDidChange:(MyCustomTextViewClass *)textView;
- (void)textViewDidBeginEditing:(MyCustomTextViewClass *)textView;
@end
???
我看不出这如何从UITextView转发代理协议
谢谢你的帮助。你的问题有点不清楚。如果您只想让MyCustomTextViewClass实现UITextViewDelegate方法,那么只需声明它即可:
@class MyCustomTextViewClass : UITextView <UITextViewDelegate> {
...
}
如果希望MyCustomTextViewClassDelegate需要与UITextViewDelegate相同的所有方法,则可以将其声明为从该协议继承,如下所示:
@class MyDelegateClass : NSObject <UITextViewDelegate, MyCustomTextViewClassDelegate> {
...
}
@protocol MyCustomTextViewClassDelegate <UITextViewDelegate> {
...
}
编辑:新答案。请理解UITextView子类不需要声明自己的委托协议。它是这样工作的:不是所有的矩形都是正方形,但是所有的正方形都是矩形。MyCustomTextView将对UITextView响应的任何消息进行操作和响应,它还将像UITextView一样向其代理发送消息。如果您想添加UITextView未涵盖的其他委托方法,那么您需要创建一个全新的协议 无论如何,考虑一下为什么需要将UITextView子类化。是否向其添加子视图,如字符计数标签?在这种情况下,创建子类将非常有用。如果您只是创建一个UITextView并更改一些公开的属性,那么定制子类可能会有些过分。我之所以问这个问题,是因为这个问题让人觉得您似乎不太理解对象继承。我不想成为一个混蛋,我想帮你 不管怎样,对代码:
//===================================
//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
}
}
我想知道你在回答的最后部分说了什么。我的问题是我在MyCustomTextViewClassDelegate的@protocol部分放了什么?类似于我在问题的最后一部分中提出的内容?您可以在@protocol部分中提出您想要扩展UITextViewDelegate的任何方法。如果您没有任何扩展UITextViewDelegate的方法,那么您不需要新的协议–您可以使用上面的第一个协议。好的,但是当它们启动时,它们将发送一个UITextView*,例如,-voidtextViewDidChange:UITextView*textView。如果他们改为发送MyCustomTextViewClass*不是更有逻辑吗?类似于-voidtextViewDidChange:MyCustomTextViewClass*textView。。。这是我的问题。如果MyCustomTextView是UITextView的一个子类,它包含UITextView的所有功能并相应地运行。你的子类的目的是什么?标准类不提供的补充内容是什么?您可能做得有些过火,或者不理解您试图实现的目标。我认为我不能声明-voidtextViewDidChange:UitextView*textView{…相反,我必须声明-voidtextViewDidChange:MyCustomTextViewClass*textView{…想想看。我在MyCustomTextViewClass内。MyCustomTextViewClass如何接收UITextView对象?我没有声明任何新协议。我只是希望在所有代理协议中使用MyCustomTextViewClass*而不是UITextView*转发来自UITextView的协议。当我在应用程序的另一个点上使用该类时在那里设置代理,我需要代理启动该类,发送MyCustomTextViewClass对象,而不是UITextView对象。您可以将传入的UITextView强制转换为MyCustomTextView。请尝试:MyCustomTextView*view=MyCustomTextView*textView;但我再次问,MyCustomTextView是什么?它的用途是什么,有什么不同?我该怎么做声明UITextView并修改其公开属性无法实现与UITextView的不同?