【委托delegate】界面传值
来源:互联网 发布:p2p监控软件 编辑:程序博客网 时间:2024/05/29 04:34
使用委托delegate传递参数的方法和 block有点类似。
委托是指给一个对象提供机会,对另一对象中的变化做出反应或者相应另一个对象的行为。其基本思想是协同解决问题。
说白了就是:B 给A 一个机会(余额信息),当我没银行没钱了(余额为0),你就知道我没钱了 ,然后B给A打钱。
在程序中:一般情况下
1.委托需要做的工作有:
1.1定义协议与方法 (@protocol .......)
1.2声明委托变量 (@property (nonatomic,weak)id<SecondViewDelegate>delegate;)
1.3设置代理 (self.delegate=...)
1.4通过委托变量调用委托方法
2.代理需要做的工作有:
2.1遵循协议 (@interface ViewController :UIViewController<SecondViewDelegate>)
2.2实现委托方法
1 如果想把信息从B传给A
2 那么B就是委托方,A就是代理方
3 通过B提出委托,A遵守协议,并且实现代理方法。
1 B提出委托
1 首先在B界面 SecondViewController.h定义委托
#import <UIKit/UIKit.h>
//B给A传参数,B是委托方,A是代理方。
//B提出委托也就是协议。
//A遵守协议委托,并实现代理方法。
@protocol SecondViewDelegate
- (void)showName:(NSString *)nameString;
@end
@interface SecondViewController :UIViewController
@property (nonatomic,weak)id<SecondViewDelegate>delegate;
@end
3在SecondViewControler.m 的按钮里面设置事件,橙色字体就是设置值。
#import <UIKit/UIKit.h>
//B给A传参数,B是委托方,A是代理方。
//B提出委托也就是协议。
//A遵守协议委托,并实现代理方法。
@protocol SecondViewDelegate
- (void)showName:(NSString *)nameString;
@end
@interface SecondViewController :UIViewController
@property (nonatomic,weak)id<SecondViewDelegate>delegate;
@end
2 遵守委托协议
4 在ViewController.h里面,也就是主页 继承协议。
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController :UIViewController<SecondViewDelegate>
@end
5 ViewController.m
#import "ViewController.h"
@interfaceViewController ()
@property (nonatomic,strong)UILabel *nameLabel;
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
//设置跳转按钮
UIButton *btn=[[UIButtonalloc]initWithFrame:CGRectMake(20,180, 130,50)];
[btn setTitle:@"跳转"forState:UIControlStateNormal];
[btn setTintColor:[UIColororangeColor]];
btn.backgroundColor=[UIColorblackColor];
[btn addTarget:selfaction:@selector(clickAction)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
self.nameLabel=[[UILabelalloc] initWithFrame:CGRectMake(20,80, 130,50)];
self.nameLabel.backgroundColor=[UIColororangeColor];
[self.viewaddSubview:self.nameLabel];
}
- (void)clickAction
{
SecondViewController *one=[[SecondViewControlleralloc]init];
[selfpresentViewController:one animated:YEScompletion:nil];
//设置代理,它就是代理方。 !!!很关键。
one.delegate =self;
}
#pragma mark 实现传值协议方法
- (void)showName:(NSString *)nameString
{
self.nameLabel.text=nameString;
}
@end
- 【委托delegate】界面传值
- 页面之间传值之delegate委托传值
- swift 设计模式---委托(delegate)模式传值
- IOS 委托代理(delegate)实现页面传值
- IOS 委托代理(delegate)实现页面传值
- IOS 委托代理(delegate)实现页面传值
- 委托(Delegate)
- 委托(Delegate)
- delegate委托
- 委托(delegate)
- Delegate 委托
- delegate 委托
- 委托:delegate
- 委托 delegate
- 委托(delegate)
- 委托,delegate
- delegate-委托
- 委托 delegate
- Spring Collections(List,Set,Map,Properties)
- js_错误处理机制
- linux下通过yum安装svn及配置
- 二分查找
- volatile
- 【委托delegate】界面传值
- spring-boot 加载本地静态资源文件路径配置
- Java Annotation
- 常见的几种移动app开发模式
- Android内存优化大全 (中篇)
- [转]关于C与C++中互相调用动态链接库的问题_卓越李子_新浪博客
- 浅谈Java中的hashcode方法
- pdf文件转换成txt格式的操作方法
- wince .net2.0.5升级2.0.7