iOS 协议delegate分六步

第一步:声明协议  
          在RootView.h中,
          @protocol 协议名 <基类>
           方法
           @end
@protocol RootViewDelegate <NSObject>
- (void)presentToViewController;
@end
第二步:声明代理人
          在RootView.h中
//必须是assign,为了防止两个对象之间的循环引用
@property (nonatomic, assign)id<RootViewDelegate>rootDelegate;
(属性)。。。。
@end
第三步:执行协议方法
在RootView.m中
- (void)buttonAction:(UIButton *)button{
   //协议第三步:执行协议方法
    [self.rootDelegate presentToViewController];
}
第四步:签订协议
在 RootViewController.m中人
//协议第四步:签订协议
@interface RootViewController ()<RootViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
(属性)。。。。
@end
第五步:设置代理人
在 RootViewController.m中人
- (void)viewDidLoad {
    [super viewDidLoad];
    self.rootV = [[RootView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview: self.rootV];
    [_rootV release];  
    //协议第五步:设置代理人
    self.rootV.rootDelegate = self;
   
}
第六步:实现协议方法
在 RootViewController.m中人
//协议第六步:实现协议方法
- (void)presentToViewController{
    //下一个页面:模态
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    [self presentViewController:firstVC animated:YES completion:^{
       
    }];
    NSLog(@"aaaaa");
}
 
 
/**
 总结协议/代理模式
 1.什么情况下用协议?
 第一个类里创建了第二个类对象,并给第二个对象传值
 当第二个类的对象要想控制第一个类里的方法,或者给第一个类传值,必须用协议
 */
 
原文地址:https://www.cnblogs.com/z-han49888/p/5076623.html