简体   繁体   中英

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;
}

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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