How to create and use Protocol , if more then one

2019-09-19 17:01发布

问题:

I Want to create A Protocol in my project For conform some parameters I have three classes to use so I want the classes to conform the protocol.

So please help me.

Thanks in advance

回答1:

I just Give Basic Idea for how to Create Protocol

Also Read This Question

#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);

}


回答2:

As you do for one class, use it for three or even dozens of classes, it doesn't matter how many protocols your class conforms to.

As you can see, i added randomly many delegate protocols here

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, NSCoding, UIAlertViewDelegate, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>

@property (strong, nonatomic) id detailItem;

@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end

To Create a protocol, refer here