简体   繁体   English

覆盖自定义类的方法。 Objective-C的

[英]Overriding methods of custom class. Objective-C

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];

    // code...
}

That's basically what I was doing to override standard methods of classes in Objective-C. 这基本上就是我在覆盖Objective-C中的标准类方法时所做的。 I have created a custom class myClass that has a variable called style NSInteger myStyle. 我创建了一个自定义类myClass,它有一个名为style NSInteger myStyle的变量。

I tried the same thing: 我试过同样的事情:

- (void)setMyStyle:(NSInteger)myStyle
{
    [super setMyStyle:myStyle];

    // code...
}

But it's not working, since it can't find this method: 但它无法正常工作,因为它找不到这种方法:

[super setMyStyle:myStyle];

Any suggestions? 有什么建议么?

But it's not working, since it can't find this method: 但它无法正常工作,因为它找不到这种方法:

As you are calling [super setMyStyle:myStyle]; 当你调用[super setMyStyle:myStyle]; your super class must have a method named setMyStyle . 你的超类必须有一个名为setMyStyle的方法。

EDIT: 编辑:

I have created a custom class myClass that has a variable called style NSInteger myStyle. 我创建了一个自定义类myClass,它有一个名为style NSInteger myStyle的变量。

As you have a variable called myStyle , you need to create setter and getter for it. 由于你有一个名为myStyle的变量,你需要为它创建setter和getter。 For this you need create methods or use @property / @synthesize . 为此,您需要创建方法或使用@property / @synthesize

Or no need to create an iVar, you can directly create a property for myStyle . 或者无需创建iVar,您可以直接为myStyle创建属性。

Make sure you have your method declared in the header file for your parent class: 确保在父类的头文件中声明了您的方法:

//ParentClass.h

@interface ParentClass : ItsParentClass {

}

- (void)setMyStyle:(NSInteger)myStyle;

And that you #include the header in your subclass 并且您#include子类中的标头

//YourSubClass.h
#import "ParentClass.h"

@interface YourSubClass : ParentClass {

}

That way when you call -[super setMyStyle:] the compiler expects super's class (ParentClass) to have the setMyStyle method and you won't get the error. 这样当你调用 - [super setMyStyle:]时,编译器希望super的类(ParentClass)具有setMyStyle方法,并且你不会得到错误。

#import "YourSublass.h"
@implementation YourSublass

- (void)setMyStyle:(NSInteger)myStyle
{
    [super setMyStyle:myStyle];

    // code...
}

@end

Another reason i could think of is, probably you forgot to include the parent class in your header class? 我能想到的另一个原因是,您可能忘记在头类中包含父类?

@interface CustomClass : ParentClass{

}

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM