我想在我的项目中创建一个协议为了符合一些参数,我要使用三个类,所以我希望这些类符合协议。
所以请帮助我。
提前致谢
我只是给出了如何创建协议的基本思路
#DetailViewController.h
#import <UIKit/UIKit.h>
@protocol MasterDelegate <NSObject>
-(void) getButtonTitile:(NSString *)btnTitle;
@end
@interface DetailViewController : MasterViewController
@property (nonatomic, assign) id<MasterDelegate> customDelegate;
#DetailViewController.m
if([self.customDelegate respondsToSelector:@selector(getButtonTitile:)])
{
[self.customDelegate getButtonTitile:button.currentTitle];
}
#MasterViewController.m
create obj of DetailViewController
DetailViewController *obj = [[DetailViewController alloc] init];
obj.customDelegate = self;
[self.navigationController pushViewController:reportTypeVC animated:YES];
and add delegate method in MasterViewController.m for get button title.
#pragma mark -
#pragma mark - Custom Delegate Method
-(void) getButtonTitile:(NSString *)btnTitle;
{
NSLog(@"%@", btnTitle);
}
正如你对一个类所做的那样,将它用于三个甚至几十个类,你的类符合多少协议并不重要。
如您所见,我在这里随机添加了许多委托协议
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, NSCoding, UIAlertViewDelegate, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end
要创建协议,请参阅此处