James White
James White

Reputation: 784

Delegate method can't reference its own class

I have a UIView subclass called NumberPickerView. I'm writing a delegate method for it. The compiler won't let me pass an instance of NumberPickerView as an parameter in that method. What am I missing?

@protocol NumberPickerViewDelegate

-(void) numberPickerDidChangeSelection:(NumberPickerView *)numberPickerView;
//error: expected a type

@end


@interface NumberPickerView : UIView <UIScrollViewDelegate> {
     id <NumberPickerViewDelegate> delegate;
}

Upvotes: 1

Views: 77

Answers (2)

Injectios
Injectios

Reputation: 2797

Actually it CAN. At that point compiler doesn't know about NumberPickerView class

@class NumberPickerView;

add it over protocol declaration to let compiler know about that class... It's called forward declaration.

For better understanding check this out:

iPhone+Difference Between writing @classname & #import"classname.h" in Xcode

OR

move protocol declaration below the class NumberPickerView definition but in that case you should also add at top:

@protocol NumberPickerViewDelegate;

Not to get warnings using id<NumberPickerViewDelegate>delegate

Upvotes: 3

user2919209
user2919209

Reputation:

You can change parameter type to id instead of NumberPickerView * and pass any class object afterword as bellow

@protocol NumberPickerViewDelegate

-(void) numberPickerDidChangeSelection:(id)numberPickerView;

@end


@interface NumberPickerView : UIView <UIScrollViewDelegate> {
     id <NumberPickerViewDelegate> delegate;
}

Upvotes: 3

Related Questions