Nick Keroulis
Nick Keroulis

Reputation: 133

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. I have created a custom class myClass that has a variable called 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?

Upvotes: 0

Views: 1665

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

But it's not working, since it can't find this method:

As you are calling [super setMyStyle:myStyle]; your super class must have a method named setMyStyle.

EDIT:

I have created a custom class myClass that has a variable called style NSInteger myStyle.

As you have a variable called myStyle, you need to create setter and getter for it. For this you need create methods or use @property/@synthesize.

Or no need to create an iVar, you can directly create a property for myStyle.

Upvotes: 1

Brad The App Guy
Brad The App Guy

Reputation: 16275

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

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

#import "YourSublass.h"
@implementation YourSublass

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

    // code...
}

@end

Upvotes: 1

lakshmen
lakshmen

Reputation: 29104

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

Upvotes: 0

Related Questions