定义自定义协议

发布于 2024-10-07 21:58:35 字数 1396 浏览 5 评论 0原文

我有一个自定义类。我希望如果任何其他类实例化它,那么它“必须”有一些特定的方法。 如何实现这一目标? 我不想继承它,因为我没有添加任何额外的功能或以任何方式修改其功能。 我想到了自定义协议,但我的类如何知道“只有当定义的协议由实例化它的类实现时,它才应该允许实例化自身。”
场景是
classA:基类 classB:基类 classM 具有 base-class 类型的属性。我将其设置为 objclassA 或 objclassB 。 A 类 & classB 实例化 classM 然后 objclassM`` 调用方法callBack方法,该方法位于classA&< /代码>B类<代码>。 classM 基类中的警告可能不会响应回调`,


@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:'

谢谢sssssss,我通过在类中定义自定义协议来摆脱这些警告


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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮坠入山谷 2024-10-14 21:58:35

您可以检查某个类是否符合给定协议,

[MyClass conformsToProtocol:@protocol(Joining)];

请参阅 内省

一个真实的例子。请注意,delegate 是在 id中定义的。 delegate;,意味着作为委托的对象应该符合协议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

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
挽心 2024-10-14 21:58:35

使用委托怎么样?

您可以将实例化您的类的对象设置为其委托。然后,在类的代码中,您可以通过调用respondsToSelector来检查委托是否具有您正在查找的方法

[delegate respondsToSelector:@selector(yourMethod)]

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)]
瑕疵 2024-10-14 21:58:35

你的头类应该是这样的:

#import "ClassA.h"
@protocol myDelegate;

@interface ClassA : UIViewController {
}
@end

@protocol myDelegate
- (void)doSomething;
@end

ClassB 应该是这样的:

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

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

如果你使用 你必须在 ClassB 中实现该方法,否则你会收到警告。

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.

独行侠 2024-10-14 21:58:35

将实例变量 backObject 的声明更改为:

id <UITableViewMgrDelegate> backObject;

如果您收到有关 classA 或 classB 不符合 UITableViewMgrDelegate 的警告,只需将其添加到它们的接口中:

@interface classA : UIViewController <UITableViewMgrDelegate>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文