iOS 代理-协议的代码笔记
2014-02-26 15:18
429 查看
1,谁要让别人给自己做事(协议方法里面获得值) 则他必须要加上这么一个代理
@interface ViewController :UIViewController<TestDelegate,DoSomeDelegate,DoSomeDataSource>
实现代理方法 等待代理传过来的数值
-(void)changeValue:(NSString *)value;
2,给别人做事 他必须有一个代理的对象
@property(strong,nonatomic)id<DoSomeDelegate>delegate;
3.给别人做什么事情呢?如下:
[self.delegatechangeValue:self.text.text];//要做的事情就是:赋值
4 源代码:
代理类:
.h
使用代理
.h
.m
download:demo
@interface ViewController :UIViewController<TestDelegate,DoSomeDelegate,DoSomeDataSource>
实现代理方法 等待代理传过来的数值
-(void)changeValue:(NSString *)value;
2,给别人做事 他必须有一个代理的对象
@property(strong,nonatomic)id<DoSomeDelegate>delegate;
3.给别人做什么事情呢?如下:
[self.delegatechangeValue:self.text.text];//要做的事情就是:赋值
4 源代码:
代理类:
.h
// // DoSomeObj.h // delegate // // Created by linpeng on 14-2-25. // Copyright (c) 2014年 linpeng. All rights reserved. // #import <Foundation/Foundation.h> //添加协议 @protocol DoSomeDelegate, DoSomeDataSource; @interface DoSomeObj : NSObject @property(strong,nonatomic)NSString *str1,*str2; @property(strong,nonatomic)id<DoSomeDelegate>delegate; @property(strong,nonatomic)id<DoSomeDataSource>datasource; -(void)Action; @end //定义一个协议 :DoSomeDelegate @protocol DoSomeDelegate <NSObject> @required -(void)doSomeDelegateAction:(NSString *)str; @end //定义一个协议 :DoSomeDataSource @protocol DoSomeDataSource <NSObject> @required -(void)doSomeDataSourceAction:(NSString *)str; @end.m
// // DoSomeObj.m // delegate // // Created by linpeng on 14-2-25. // Copyright (c) 2014年 linpeng. All rights reserved. // #import "DoSomeObj.h" @implementation DoSomeObj -(id)init { if(self=[super init]) { self.str1 = @"delegate"; self.str2 = @"datasource"; //放着里是没用的 因为这个时候的代理===是空的!!!! // [self.delegate doSomeDelegateAction:@"delegate"]; // [self.datasource doSomeDataSourceAction:@"datasource"]; } return self; } //促发代理做一些处理 -(void)Action { //前面 DoSomeObj *doSome = [[DoSomeObj alloc] init];doSome.delegate = self; 所以他的代理不是空的 [self.delegate doSomeDelegateAction:self.str1]; [self.datasource doSomeDataSourceAction:self.str2]; } @end
使用代理
.h
#import <UIKit/UIKit.h> #import "TestDelegate.h" #import "DoSomeObj.h" @interface ViewController : UIViewController<TestDelegate,DoSomeDelegate,DoSomeDataSource> @end
.m
//第二个代理 DoSomeObj *doSome = [[DoSomeObj alloc] init]; doSome.delegate = self; doSome.datasource = self;
//触发代理类赋值 [doSome Action];
#pragma 协议方法 -(void)doSomeDataSourceAction:(NSString *)str { lab.text = [NSString stringWithFormat:@"%@--%@",lab.text,str]; } #pragma 协议方法 -(void)doSomeDelegateAction:(NSString *)str { lab.text = [NSString stringWithFormat:@"%@--%@",lab.text,str]; }
download:demo
相关文章推荐
- IOS学习笔记-协议和代码块
- <<Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法>>笔记-协议与分类
- IOS学习笔记(页面传值:属性传值,协议代理传值,闭包传值)
- iOS复习笔记11:协议和代理
- 【代码笔记】iOS-MBProgressHUD
- IOS学习之一个示例弄懂代理(delegate)和协议
- IOS代码笔记UIView的placeholder的效果
- 【iOS学习笔记】代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- 【代码笔记】iOS-获得现在的时间
- iOS开发笔记--使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
- 论ios代理和协议
- 关于iOS 委托 代理 协议 的理解
- iOS开发笔记--代理方法
- 【代码笔记】iOS-UIActionSheet字体的修改
- [IOS]Protocol协议及委托代理(Delegate)传值-用代码寻找storyboard相应视图
- iOS 水纹效果代码笔记
- 【代码笔记】iOS-下拉菜单
- 【代码笔记】iOS-iOS图片的原生(Graphics)
- 【代码笔记】iOS-账号,密码记住
- 【代码笔记】iOS-字符串的分割