[英]Delegate method can't reference its own class
I have a UIView subclass called NumberPickerView. 我有一个名为NumberPickerView的UIView子类。 I'm writing a delegate method for it. 我正在为此编写委托方法。 The compiler won't let me pass an instance of NumberPickerView as an parameter in that method. 编译器不允许我将NumberPickerView的实例作为该方法的参数传递。 What am I missing? 我想念什么?
@protocol NumberPickerViewDelegate
-(void) numberPickerDidChangeSelection:(NumberPickerView *)numberPickerView;
//error: expected a type
@end
@interface NumberPickerView : UIView <UIScrollViewDelegate> {
id <NumberPickerViewDelegate> delegate;
}
Actually it CAN. 实际上可以。 At that point compiler doesn't know about NumberPickerView
class 那时编译器不知道NumberPickerView
类
@class NumberPickerView;
add it over protocol declaration to let compiler know about that class... It's called forward declaration
. 在协议声明上添加它,以使编译器知道该类...这称为forward declaration
。
For better understanding check this out: 为了更好地理解,请查看以下内容:
iPhone+Difference Between writing @classname & #import"classname.h" in Xcode iPhone +在Xcode中编写@classname和#import“ classname.h”之间的区别
OR 要么
move protocol declaration below the class NumberPickerView
definition but in that case you should also add at top: 将协议声明移到NumberPickerView
类的定义下方,但在这种情况下,还应该在顶部添加:
@protocol NumberPickerViewDelegate;
Not to get warnings using id<NumberPickerViewDelegate>delegate
不使用id<NumberPickerViewDelegate>delegate
获得警告
You can change parameter type to id instead of NumberPickerView * and pass any class object afterword as bellow 您可以将参数类型更改为id,而不是NumberPickerView *,然后将任何类对象后缀传递为
@protocol NumberPickerViewDelegate
-(void) numberPickerDidChangeSelection:(id)numberPickerView;
@end
@interface NumberPickerView : UIView <UIScrollViewDelegate> {
id <NumberPickerViewDelegate> delegate;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.