[英]how subclass extending the parent class delegate?
subclass extending the parent class delegate,and subclass worked,but have a warning!!! 子类扩展了父类的委托,并且子类起作用了,但是要警告!!!
parent class: 家长班:
@protocol ObjADelegate;
@interface ObjA : NSObject
@property (nonatomic, assign) id<ObjADelegate> delegate;
- (void)doSth1;
@end
@protocol ObjADelegate <NSObject>
- (void)seeA;
@end
subclass: 子类:
#import "ObjA.h"
@protocol ObjBDelegate;
@interface ObjB : ObjA
@property (nonatomic, assign) id<ObjBDelegate,ObjADelegate> delegate;
- (void)dosth2;
@end
@protocol ObjBDelegate <ObjADelegate>
- (void)seeB;
@end
it's worked but have a warning for class objB 's delegate : auto property synthesis will not synthesize property 'delegate';it will be implemented by its superclass , use @dynamic to acknowledge intention
它是可行的,但会警告objB类的委托:
auto property synthesis will not synthesize property 'delegate';it will be implemented by its superclass , use @dynamic to acknowledge intention
how can i remove this warning????? 我如何删除此警告?????
The hint is in the warning message. 提示在警告消息中。 You need to use
@dynamic
in your implementation to tell the compiler that the properties setters and getters are handled elsewhere (in this case, by the superclass). 您需要在实现中使用
@dynamic
来告诉编译器,属性设置器和获取器在其他地方处理(在这种情况下,由超类处理)。
@implementation ObjB
@dynamic delegate;
- (void)dosth2
{
}
@end
parent class: .h 父类:.h
#import <Foundation/Foundation.h>
@protocol ClassADelegate;
@interface ClassA : NSObject{
id _delegate;
}
@property (nonatomic, assign) id<ClassADelegate> delegate;
- (void)fun1;
@end
@protocol ClassADelegate <NSObject>
- (void)hello;
@end
parent class:.m 父类:.m
#import "ClassA.h"
@implementation ClassA
@dynamic delegate;
- (void)fun1{
if (self.delegate && [self.delegate respondsToSelector:@selector(hello)]) {
[self.delegate hello];
}
}
- (void)setDelegate:(id<ClassADelegate>)delegate{
_delegate = delegate;
}
- (id<ClassADelegate>)delegate{
return _delegate;
}
@end
subclass:.h 子类:.H
#import "ClassA.h"
@protocol ClassBDelegate <NSObject, ClassADelegate>
-(void)nihao;
@end
@interface ClassB : ClassA
@property (nonatomic, assign) id<ClassBDelegate> delegate;
- (void)fun2;
@end
subclass:.m 子类:.M
#import "ClassB.h"
@implementation ClassB
@dynamic delegate;
- (void)fun2{
if (_delegate && [_delegate respondsToSelector:@selector(nihao)]) {
[_delegate nihao];
}
}
- (void)setDelegate:(id<ClassBDelegate>)delegate{
_delegate = delegate;
}
- (id<ClassBDelegate>)delegate{
return _delegate;
}
@end
subclass delegate statement must on header 子类委托语句必须在标头上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.