Overriding superclass method in Objective-C

时间:2022-12-01 22:53:52

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."

所以,我创建了我的类“A”的子类,并将其称为“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我想从我的类“A”覆盖setupModalView方法。我这样做了:

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).

我认为它应该有效,但事实并非如此。我究竟做错了什么? (澄清一下:我希望B类的setupModalView与A类的setupModalView完全不同)。

Edit: I'm instantiating ClassB like this:

编辑:我正在实例化这样的ClassB:

 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:

编辑2:这是我对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;
}

2 个解决方案

#1


3  

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.

这是错的。你已经分配了self,现在你明确地将它变成了ClassA的一个实例。如果initWithFrame是您指定的初始化程序,那么当您最初为自己分配内容时,需要调用它而不是[super init]。

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...

而不是[super init],你需要编写[self initWithFrame ...

#2


0  

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.

NSLog输出2013-09-26 23:03:12.817测试[41586:a0b]完成。

#1


3  

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.

这是错的。你已经分配了self,现在你明确地将它变成了ClassA的一个实例。如果initWithFrame是您指定的初始化程序,那么当您最初为自己分配内容时,需要调用它而不是[super init]。

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...

而不是[super init],你需要编写[self initWithFrame ...

#2


0  

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.

NSLog输出2013-09-26 23:03:12.817测试[41586:a0b]完成。