IOS传值之代理传值(一)
1、使用代理delegate的方法
2、使用通知Notification的方法
3、KVO等方法
4、block传值
~~~~~~~~~~~~~~~~
1、使用代理delegate的方法
#import "ViewController.h"
#import "SubViewController.h"
@interface ViewController ()<subViewDelegate>
@property(nonatomic,strong)UILabel *label;
@end
@implementation ViewController
-(UILabel *)label{
if (_label==nil) {
_label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
_label.backgroundColor=[UIColor grayColor];
//[self.view addSubview:_label];
}
return _label;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.label];
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:@"next" forState:UIControlStateNormal];
[button addTarget:self action:@selector(nextVC) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)nextVC{
NSLog(@"hhhhhhhhh");
SubViewController *subVC=[[SubViewController alloc]init];
subVC.delegate=self;
//[self.navigationController pushViewController:subVC animated:YES];
[self presentViewController:subVC animated:YES completion:^{
NSLog(@"cccccc");
}];
}
-(void)showStringFromSubView:(NSString *)textFieldtext{
self.label.text=textFieldtext;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
~~~~~~~~~~~~~~~~~~~~~~~
#import <UIKit/UIKit.h>
@protocol subViewDelegate <NSObject>
-(void)showStringFromSubView:(NSString *)textFieldtext;
@end
@interface SubViewController : UIViewController
@property(nonatomic,weak)id<subViewDelegate> delegate;
@end
#import "SubViewController.h"
@interface SubViewController ()
@property(nonatomic,strong)UITextField *textField;
@end
@implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.textField];
//backBtn
UIButton *back= [UIButton buttonWithType:UIButtonTypeCustom];
back.frame=CGRectMake(100, 200, 50, 50);
[back setBackgroundColor:[UIColor grayColor]];
[back setTitle:@"back" forState:UIControlStateNormal];
[back addTarget:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
}
-(void)backToPre{
[self.delegate showStringFromSubView:self.textField.text];
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"backToPre");
}];
}
-(UITextField *)textField{
if (_textField==nil) {
_textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
_textField.backgroundColor=[UIColor grayColor];
}
return _textField;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~实现效果:




IOS传值之代理传值(一)的更多相关文章
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...
- iOS 再谈 代理传值,block反向传值
本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...
- Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值
一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...
- IOS pop使用代理传值
假如oneViewController页面push到OtherViewController页面,然后你想从OtherViewController页面pop到oneViewController页面的时候 ...
- iOS 学习 - 1.代理传值
代理的目的是改变或传递控制链.允许一个类在某些特定时刻通知到其他类,而不需要获取到那些类的指针.可以减少框架复杂度. 另外一点,代理可以理解为java中的回调监听机制的一种类似 优点:1.避免子类化带 ...
- iOS 闭包传值 和 代理传值
let vc = ViewController() let navc = UINavigationController(rootViewController: vc) window = UIWindo ...
- block传值和代理传值的异同点
delegate:1,“一对一”,对同一个协议,一个对象只能设置一个代理delegate,所以单例对象就不能用代理:2,代理更注重过程信息的传输:比如发起一个网络请求,可能想要知道此时请求是否已经开始 ...
- iOS 页面间传值 之 属性传值,代理传值
手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...
- iOS的四种传值方式
传值有四种方法 : 1. 属性传值 2. 单例传值 3. 代理传值 4. block传值 一.属性传值 (前-->后) 1. 后面的界面定义一个属性 存放前一个界面传过来的值 ...
随机推荐
- 统计C语言程序行数
补充前一篇中统计C语言程序行数的程序 写得比较匆忙,可能有些失误,等弄明白GitHub的用法并完善程序后再补充完整代码链接 没有写成函数,但经过简单修改可以作为一个计算或判断函数使用 判断算法主要为以 ...
- 4、BufferedIn(out)putStream--->字节输入/输出流的缓冲区类(高效类:高效率读写)
前言 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流 ...
- Effective JavaScript :第五章
1.使用Object的直接实例构造轻量级的字典 字典就是可变长的字符串与值得映射集合.JavaScript甚至提供了枚举一个对象属性名的利器——for...in循环. var dict = { ali ...
- reshape2 数据操作 数据融合 (melt)
前面一篇讲了cast,想必已经见识到了reshape2的强大,当然在使用cast时配合上melt这种强大的揉数据能力才能表现的淋漓尽致. 下面我们来看下,melt这个函数以及它的特点. melt(da ...
- hdu_5883_The Best Path(欧拉路)
题目链接:hdu_5883_The Best Path 题意: n 个点 m 条无向边的图,找一个欧拉通路/回路使得这个路径所有结点的异或值最大. 题解: 节点 i 的贡献为((du[i] +1/ 2 ...
- 利用scrollTop 制作图片无缝滚动
<!doctype html><title>javascript无缝滚动 by 司徒正美</title><meta charset="utf-8&q ...
- PowerDesigner 的mysql PDM 的COMMENT注释
PowerDesigner 的mysql PDM 的COMMENT注释 2012-11-01 15:38 4447人阅读 评论(0) 举报 分类: 数据库相关(7) PowerDesigner 的my ...
- log4j2日志
log4j2.xmllog4j-api-2.5.jarlog4j-core-2.5.jar <?xml version="1.0" encoding="UTF-8& ...
- Spring框架--AOP编程
2 手动实现AOP编程 AOP 面向切面的编程, AOP可以实现"业务代码"与"关注点代码"分离 // 保存一个用户 public void add(User ...
- touchesBegan: withEvent: <--- with UIScrollView / UIImageView
touchesBegan: withEvent: / touchesMoved: withEvent: / touchesEnded: withEvent: 等只能被UIView捕获(如有问题请指出对 ...