iphoneobjective-ccustom-protocol

defining custom protocol


I have a custom class.And I want if any other class instantiate it then it "must" have some specific method. How to achieve this? I don't want to inherit it cause I m not adding any extra feature or modifying its functionality in any way. I thought of custom protocol but how will my class know that "it should allow to instantiate itself only if defined protocol is implemented by class being instantiating it."
scenario is
classA : base-class classB : base-class classM has property of type base-class. which i set as objclassA or objclassB. ClassA & classB instantiate classM then objclassM`` calls methodcallBackmethod which is in bothclassA&classB. warning inclassM base-class may not response to callBack`


@protocol UITableViewMgrDelegate
@required
-(void)tableRowSelected:(int)idd selectedType:(NSString*)selectedType selectedValue:(NSString*)selectedValue;
@end
@interface UITableViewMgr : UIViewController   {

    NSMutableArray *dataSo,*IDs;
    NSMutableArray *dataSoRight;
    UIViewController *backObject;
}
in .m
[backObject tableRowSelected:(NSInteger)[indexPath row] selectedType:[NSString stringWithFormat:@"cell"] selectedValue:[NSString stringWithFormat:@"cell"]];
//warning at this line 
// 'UIViewController' may not respond to '-tableRowSelected:selectedType:selectedValue:'

thankssssssss I got rid off those warnings by defining custom protocol in my class this way


@protocol UITableViewMgrDelegate
@required
-(void)tableRowSelected:(int)idd selectedType:(NSString*)selectedType selectedValue:(NSString*)selectedValue;
@optional 
- (void)AddList:(NSString*)value isNew:(int)isNew;
@end


Solution

  • You can check if a certain class conforms to a given protocol

    [MyClass conformsToProtocol:@protocol(Joining)];
    

    see Introspection

    A real-word example. Note that the delegate is defined id<VSKeypadViewDelegate> delegate;, what means that an object, that is meant to be the delegate should conform to the protocol VSKeypadViewDelegate

    #import <UIKit/UIKit.h>
    @protocol VSKeypadViewDelegate
    @required
    -(int)numberOfRows;
    -(int)numberOfColumns;
    
    -(NSString*)titleForButtonOnRow:(int)row andColumn:(int)column;
    -(id)valueForButtonOnRow:(int)row andColumn:(int)column;
    -(CGSize)sizeForButtonOnRow:(int)row andColumn:(int)column;
    -(void)receivedValue:(id)value;
    -(CGPoint)keypadOrigin;
    
    @optional
    -(NSArray *)additionalButtonsForKeypad;
    //-(UIColor *)keypadBackgroundColor;
    //-(UIColor *)keyBackgroundColorForRow:(int)row andColumn:(int)Column;
    -(UIImage *)backgroundImageForState:(UIControlState)state forKeyAtRow:(int)row andColumn:(int)column;
    -(BOOL)isButtonEnabledAtRow:(int)row andColumn:(int)column;
    
    @end
    
    
    @interface VSKeypadView : UIView {
        id<VSKeypadViewDelegate> delegate;
        NSArray *keypadButtons;
    }
    
    + (VSKeypadView *)keypadViewWithFrame:(CGRect)r;
    
    - (id)initWithFrame:(CGRect)r ;
    -(void)fireKeypadButton:(id)sender;
    
    @property(nonatomic, assign) id<VSKeypadViewDelegate> delegate;
    
    @end