Reputation: 858
I have a hierarchy of UIViews. They are all handled differently but if nested I can not get my setDelegate of super to fire. I receive a crash exception [ThirdClass setDelegate:] unrecognized selector sent to instance. This actually happens no matter what (subclass) i use SecondClass or ThirdClass, but If I use (FirstClass) everything works as it should but any subclassing of the delegate it does not recognize the call. I have simplified what I am doing below which if I call out my first class separately inside my MainControlInterface everything works as it should. Im sure Im doing something wrong here but can't determine what that is, If anyone could help that would be greatly appreciated, thank you.
@protocol FirstClassDataSource, FirstClassDelegate;
@interface FirstClass : UIView
@property (nonatomic, weak_delegate) __nullable id<FirstClassDataSource> dataSource;
@property (nonatomic, weak_delegate) __nullable id<FistClassDelegate> delegate;
@end
@protocol FirstClassDataSource <NSObject>
- (NSInteger)doSomething:(FirstClass *)class;
@optional
- (NSInteger)doSomethingElse:(FirstClass *)class;
@end
@protocol FirstClassDelegate <NSObject>
@optional
- (void)handleMoreDelegateMethods:(FirstClass *)class;
@end
@implementation FirstClass
- (void)setDataSource:(id< FirstClassDataSource >)dataSource
{
if (_dataSource != dataSource)
{
_dataSource = dataSource;
if (_dataSource)
{
[self reloadData];
}
}
}
- (void)setDelegate:(id< FirstClassDelegate>)delegate
{
if (_delegate != delegate)
{
_delegate = delegate;
if (_delegate && _dataSource)
{
[self setNeedsLayout];
}
}
}
@end
@interface SecondClass : FirstClass
-(id)sencondClassesPrivateMethods;
@end
@interface ThirdClass : secondClass
-(id)thirdClassPrivateMethods;
@end
@interface MainControlInterface : UIView <FirstClassDataSource, FirstClassDelegate>
-(ThirdClass *)thirdClass;
@end
@implementation MainControlInterface
-(void)didMoveToSuperview{
ThirdClass *mythirdSubClass = [self thirdClass];
mythirdSubClass.delegate = self;
mythirdSubClass.dataSource = self;
}
@end
Upvotes: 0
Views: 48
Reputation: 2505
I can't tell what you're doing wrong either. But, your sample code will not compile. (It's full of typos.) I have tried to recreate what you're talking about, simplifying it further. (I've used CodeRunner, a macOS app which facilitates this sort of thing.)
#import <Foundation/Foundation.h>
@protocol FirstClassHandling <NSObject>
- (void)doTheThing;
@end
@interface FirstClass : NSObject
@property (nonatomic, weak) id<FirstClassHandling> delegate;
- (void)doSomething;
@end
@implementation FirstClass
- (void)doSomething
{
NSLog(@"First class.");
if ([[self delegate] respondsToSelector:@selector(doTheThing)]) {
[[self delegate] doTheThing];
}
}
@end
@interface SecondClass : FirstClass
@end
@implementation SecondClass
- (void)doSomething
{
NSLog(@"Second class");
[super doSomething];
}
@end
@interface Handler : NSObject <FirstClassHandling>
@end
@implementation Handler
- (void)doTheThing
{
NSLog(@"Doing my thing!!!");
}
@end
int main(int argc, char *argv[])
{
@autoreleasepool {
Handler* handler = [[Handler alloc] init];
SecondClass* sc = [[SecondClass alloc] init];
sc.delegate = handler;
[sc doSomething];
}
}
The above does not crash. Please fix your example code.
Upvotes: 1