I'm trying to save on code duplication. Using composition I'm trying to provide inheritance to both UIViewControllers and NSObject classes.
I'm my scenario I'm providing code to app delegate, several other classes and UIViewControllers. The code is an implementation of a protocol / delegate.
Previously I have several functions in each of view controllers etc, which was a lot of duplicated code. Init and CrashLog are just two of the functions provided in the protocol.
Here's my setup...
iCloudDBViewController.h
@interface iCloudDBViewController : UIViewController <iCloudDBDelegate>
@property (weak,nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;
-(id)init:(id<iCloudDBDelegate>)delegate;
@end
iCloudDBViewController.m
#import "iCloudDBViewController.h"
@interface iCloudDBViewController ()
@end
@implementation iCloudDBViewController
@synthesize iCloudDBDelegate;
-(id)init:(id<iCloudDBDelegate>)delegate
{
//snip
}
-(void)crashLog:(NSString*)message, ...
{
//snip
}
@end
iCloudDBDelegateImplemation.h
@interface iCloudDBDelegateImplemation : NSObject <iCloudDBDelegate>
@property (weak,nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;
-(id)init:(id<iCloudDBDelegate>)delegate;
@end
iCloudDBDelegateImplemation.m
@implementation iCloudDBDelegateImplemation
@synthesize iCloudDBDelegate;
-(id)init:(id<iCloudDBDelegate>)delegate
{
//snip
}
-(void)crashLog:(NSString*)message, ...
{
//snip
}
@end
AppDelegate.h
@interface AppDelegate : iCloudDBDelegateImplemation <iCloudDBDelegate>
{
}
AppDelegate.m
@interface AppDelegate ()
@end
@implementation AppDelegate
@end
RootViewController.h
@interface RootViewController : iCloudDBViewController
{
}
@end
RootViewController.m
@interface RootViewController ()
@end
@implementation RootViewController
//snip
@end
Startup.h
@interface Startup : iCloudDBDelegateImplemation
@end
Startup.m
@implementation Startup
//snip
@end
You can see above I have a lot of duplicate code in iCloudDBViewController.m and iCloudDBDelegateImplemation.m.
Here's what I've tried to tried and combine NSObject into UIViewController...
iCloudDBViewController.h
@interface iCloudDBViewController : iCloudDBDelegateImplemation
<UIResponderStandardEditActions,
NSCoding,
UIAppearanceContainer,
UITraitEnvironment,
UIContentContainer,
UIFocusEnvironment>
@property (weak,nonatomic) id<iCloudDBDelegate>iCloudDBDelegate;
-(id)init:(id<iCloudDBDelegate>)delegate;
@end
iCloudDBDelegateImplemation.m
#import "iCloudDBViewController.h"
@interface iCloudDBViewController ()
@end
@implementation iCloudDBViewController
@synthesize iCloudDBDelegate;
// Here
-(id)init:(id<iCloudDBDelegate>)delegate
{
//snip
}
-(void)crashLog:(NSString*)message, ...
{
//snip
}
@end
Where I've marked Here, I added the stub method Xcode suggestions, but am getting a lot of errors from my view controllers, see screenshot.
I looked at UIViewController and found these delegates :- NSCoding, UIAppearanceContainer, UITraitEnvironment, UIContentContainer, UIFocusEnvironment.
Then UIResponder (it's parent had this delegate) :- UIResponderStandardEditActions.
So I've tried to create my own UIViewController, but I'm not sure how to proceed?
from Obj-C, multiple inheritance using composition - issues using NSObject delegates to provide a UIViewController?

No comments:
Post a Comment