Some code I inherited has an annoying warning. It declares a protocol and then uses that to specify the delegate
@protocol MyTextFieldDelegate;
@interface MyTextField: UITextField
@property (nonatomic, assign) id<MyTextFieldDelegate> delegate;
@end
@protocol MyTextFieldDelegate <UITextFieldDelegate>
@optional
- (void)myTextFieldSomethingHappened:(MyTextField *)textField;
@end
Classes which use myTextField
implement the MyTextFieldDelegate
and are called it with this code:
if ([delegate respondsToSelector:@selector(myTextFieldSomethingHappened:)])
{
[delegate myTextFieldSomethingHappened:self];
}
This works, but creates the (legitimate) warning: warning: property type 'id' is incompatible with type 'id' inherited from 'UITextField'
Here are the solutions I've come up with:
Is there a way to define the delegate property such that the compiler is happy?
try:
@property (nonatomic, assign) id<UITextFieldDelegate,MyTextFieldDelegate> delegate;
UITextField has also got property named delegate, but it has another type. Just rename your delegate
property to something else.
Found the answer in UITableView.h.
The UIScrollView has property name delegate, and the UITableView has the same name property.
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
// Your code
......
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With