-1

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

iPatel
  • 46,010
  • 16
  • 115
  • 137

2 Answers2

2

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

}
Community
  • 1
  • 1
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • Suggestion: declare the method optional in the protocol since you are showing an example of testing for the method, which should only be necessary for an optional protocol. – gaige Mar 26 '13 at 05:22
0

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

Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140