0

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

saurabh
  • 530
  • 1
  • 8
  • 22

4 Answers4

0

How about using a delegate?

You would set the object that is instantiating your class as its delegate. Then, in your class's code, you could check to see if the delegate has the method you are looking for by calling respondsToSelector

[delegate respondsToSelector:@selector(yourMethod)]
D.C.
  • 15,340
  • 19
  • 71
  • 102
  • okyeee... i thought of delegate but apple says it is to be used to deal with user interaction so i switched to protocol.Currently what i have done is setting an object( that instantiate my concern class ) as a property of class and calling method of that object. and it obviously works but it give me warning: may not respond to "method name" If I do your way. Will get rid of those warning? – saurabh Dec 18 '10 at 08:42
  • @saurabh: You must define the method in the header file. Check u did or not? – EmptyStack Dec 18 '10 at 08:43
  • scenario is classA : base-class classB : base-class classM has property of type base-class. ClassA & classB instantiate classM then objclassM calls method callBack method which is in both classA & classB. warning in classM base-class may not response to callBack – saurabh Dec 18 '10 at 08:57
0

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
Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0

your header class should look like this:

#import "ClassA.h"
@protocol myDelegate;

@interface ClassA : UIViewController {
}
@end

@protocol myDelegate
- (void)doSomething;
@end

and ClassB something like this:

#import "ClassB.h"
#import "ClassA.h"

@interface ClassB : UIViewController <myDelegate> {
}
@end

if you use <myDelegate> you have to implement the method in ClassB otherwise you get a warning.

0

Change your declaration of the instance variable backObject to:

id <UITableViewMgrDelegate> backObject;

If you get warnings about classA or classB not conforming to UITableViewMgrDelegate, just add it to their interfaces:

@interface classA : UIViewController <UITableViewMgrDelegate>
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123