如果多于一个类实现,如何创建和使用协议

时间:2013-03-26 04:37:06

标签: iphone ios objective-c xcode cocoa-touch

我想在我的项目中创建一个协议为了符合一些参数,我有三个类要使用,所以我希望这些类符合协议。

所以请帮助我。

提前致谢

2 个答案:

答案 0 :(得分:2)

我只是提出了如何创建协议

的基本想法

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

}

答案 1 :(得分:0)

正如您针对一个课程所做的那样,将它用于三个甚至几十个课程,您的课程符合的协议数量无关紧要。

如您所见,我在这里随机添加了许多代理协议

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

@property (strong, nonatomic) id detailItem;

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

要创建协议,请参阅here