Reputation: 19969
Class A.h
@protocol ClassADelegate;
@interface ClassA : UIView
// Some properties
- (void)setupModalView;
@end
@protocol ClassADelegate <NSObject>
// Some delegate methos
@end
Class A.m
- (void)setupModalView{
// Some code
}
So, I created a subclass of my class "A," and called it "B."
B.h
@interface ClassB : ClassA
@end
So, on B.m
I want to override setupModalView
method from my class "A." I did this:
B.m
- (void)setupModalView{
NSLog(@"Done.");
}
I think it should work, however it doesn't. What am I doing wrong? (To clarify: I want setupModalView
on class B to do something completely different from setupModalView
on class A).
Edit: I'm instantiating ClassB like this:
ClassB *classB = ({
[[ClassB alloc]initWithNotification:nil]; // Notification is a custom NSObject... nvm
});
classB.delegate = self;
[classB show];
Edit 2: Here's my init methods for ClassA:
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (instancetype)initWithNotification:(Notification *)notification{
self = [super init];
if (self) {
self = [[ClassA alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self configureDetailView];
}
return self;
}
- (instancetype)init{
self = [super init];
if (self) {
}
return self;
}
Upvotes: 0
Views: 4531
Reputation: 119292
self = [[ClassA alloc]initWithFrame:[UIScreen mainScreen].bounds];
This is wrong. You've already assigned self, now you're explicitly making it an instance of ClassA. If initWithFrame is your designated initialiser, then you need to call it instead of [super init]
when you originally assign something to self.
Never use an explicit class name in an initialiser - that will make your class unsubclassable.
Instead of [super init]
, you need to write [self initWithFrame...
Upvotes: 3
Reputation: 112873
Here is working example code:
#import "AppDelegate.h"
@protocol ClassADelegate;
@interface ClassA : UIView
- (void)setupModalView;
@end
@implementation ClassA
- (void)setupModalView {
NSLog(@"Super..");
}
@end
@protocol ClassADelegate <NSObject>
- (void)SomeDelegateMethod;
@end
@interface ClassB : ClassA
@end
@implementation ClassB
- (void)setupModalView {
NSLog(@"Done.");
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[ClassB new] setupModalView];
return YES;
}
@end
NSLog output
2013-09-26 23:03:12.817 Test[41586:a0b] Done.
Upvotes: 0