码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 协议delegate分六步

时间:2015-12-25 18:41:32      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

第一步:声明协议  
          在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.什么情况下用协议?
 第一个类里创建了第二个类对象,并给第二个对象传值
 当第二个类的对象要想控制第一个类里的方法,或者给第一个类传值,必须用协议
 */
 

iOS 协议delegate分六步

标签:

原文地址:http://www.cnblogs.com/z-han49888/p/5076623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!