iOS 之 代理五步骤
来源:互联网 发布:fineui java 编辑:程序博客网 时间:2024/05/08 12:40
在需要传值的页面的.h文件中先声明协议和代理方法(代理方法不唯一,可以多个)、设定一个代理人
#import <UIKit/UIKit.h>
#warning第一步声明协议及代理方法
@protocol ChangeColorDelegate <NSObject>
- (void)changeColor;
@end
@interface AView : UIView
#warning第二步设置代理人
@property (nonatomic, assign)id<ChangeColorDelegate>colorDelegate;
@end
在需要传值的页面的.m文件中
#warning第三步执行代理方法 一定要写在触发方法里面 如果是按钮就写在点击按钮后触发的方法里
//对需要传值的页面的.m文件进行初始化,并且建立一个button,点击button进行代理传值
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 100, 40, 40);
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor yellowColor];
[self addSubview:button];
}
return self;
}
- (void)buttonAction:(UIButton *)button
{
//代理人执行代理方法必须写到所触发的方法里
[self.colorDelegate changeColor];
}
@end
#import <UIKit/UIKit.h>
#import "AView.h"
#warning 第四步 签订协议
@interface RootViewController : UIViewController <ChangeColorDelegate>
@property (nonatomic, retain) AView *aView;
@end
在被传值的页面的.m文件中指定代理人以及实现代理方法,内存问题不要忘记
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)dealloc
{
[_aView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor lightGrayColor];
self.aView = [[AView alloc]initWithFrame:CGRectMake(40, 40, 300, 300)];
self.aView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.aView];
[self.aView release];
#warning第五步指定代理人
self.aView.colorDelegate = self;
}
#warning第六步实现代理方法
- (void)changeColor
{
NSLog(@"有人点我了, 我要改你里面的东西");
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- iOS 之 代理五步骤
- ios代理撰写步骤
- iOS 设置代理详细步骤
- iOS代理方法的实现步骤
- IOS之简单代理
- ios之代理
- iOS 代理,代理是实现反向传值步骤
- iOS网络编程:五、Post代理异步
- 设计模式之五 --- 代理(Proxy)模式
- 设计模式之五 --- 代理(Proxy)模式
- 设计模式之五 --- 代理(Proxy)模式
- 设计模式之五 --- 代理(Proxy)模式
- (五)sping之CGLIB动态代理
- 设计模式培训之五:代理模式
- 设计模式培训之五:代理模式
- 设计模式系列之五:代理模式
- 设计模式之五 --- 代理(Proxy)模式
- java设计模式之五:代理模式
- PHP Extension 开发(C语言版本)
- 环信sdk发送位置信息一直失败
- android 使用getIdentifier()获取资源Id
- Tengine——基础知识
- c++获得本地时间
- iOS 之 代理五步骤
- mysql查询统计连接情况
- 接口与抽象类的区别
- hibernate.hbm2ddl.auto配置详解
- MongDB基础学习(四)
- XSLT中访问当前系统的环境变量值和JVM系统属性
- 51信用卡的日志分析变迁史和技术细节
- Mac下安装QT版wireshark
- 分支-07. 比较大小(10)*