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传值之代理传值(一)的更多相关文章

  1. iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值

    有时候我们在页面跳转的时候回传递相应的参数,如,你想把在第一个页面的文本框里的内容显示在第二个文本框中,或者你又想把第二个文本框中的内容改变之后到第一个页面的文本框中,所有,这个时候我们就要用到页面跳 ...

  2. iOS 再谈 代理传值,block反向传值

    本贴的例子是:有A和B两个界面,要实现的效果就是先让A跳转到B,然后B中有个颜色的参数,当B跳转到A时,把这个颜色的参数传递给A,在A中利用这个颜色改变自己界面的颜色. 第1步:在发送者(界面B)中, ...

  3. Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值

    一.单例模式 单例模式是设计模式中最简单的一种,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象. 关于单例,有三个重要的准则需要牢 ...

  4. IOS pop使用代理传值

    假如oneViewController页面push到OtherViewController页面,然后你想从OtherViewController页面pop到oneViewController页面的时候 ...

  5. iOS 学习 - 1.代理传值

    代理的目的是改变或传递控制链.允许一个类在某些特定时刻通知到其他类,而不需要获取到那些类的指针.可以减少框架复杂度.
另外一点,代理可以理解为java中的回调监听机制的一种类似 优点:1.避免子类化带 ...

  6. iOS 闭包传值 和 代理传值

    let vc = ViewController() let navc = UINavigationController(rootViewController: vc) window = UIWindo ...

  7. block传值和代理传值的异同点

    delegate:1,“一对一”,对同一个协议,一个对象只能设置一个代理delegate,所以单例对象就不能用代理:2,代理更注重过程信息的传输:比如发起一个网络请求,可能想要知道此时请求是否已经开始 ...

  8. iOS 页面间传值 之 属性传值,代理传值

    手机 APP 运行,不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代理传值. 属性传值:属性传值是最简单,也是最常见的一种传值方式,但 ...

  9. iOS的四种传值方式

    传值有四种方法 : 1. 属性传值 2. 单例传值 3. 代理传值 4. block传值     一.属性传值   (前-->后) 1. 后面的界面定义一个属性  存放前一个界面传过来的值    ...

随机推荐

  1. NYIST OJ 题目42 一笔画问题

    水题.无向图欧拉通路的判定.用并查集判定是不是连通图! #include<cstdio> #include<cstring> #include<cmath> #in ...

  2. iOS开发app上架流程之证书的制作

    1.证书的制作:登陆 https: 1.1appid的注册 选择Identifiers 下的App IDs然后如图所示 点击加号,进入 App ID Description下的Name:这个是appI ...

  3. DD应用实例

    1.将本地的/dev/hdb整盘备份到/dev/hdd dd if=/dev/hdb of=/dev/hdd2.将/dev/hdb全盘数据备份到指定路径的image文件dd if=/dev/hdb o ...

  4. Qt_DX

    #ifndef MY_FRAME__HH__ #define MY_FRAME__HH__ #include <QtGui/QWidget> struct IDirect3D9; stru ...

  5. Docker 安装完启动服务报错

    [root@lh- ~]# docker images Cannot connect to the Docker daemon. Is the docker daemon running on thi ...

  6. kettle连接Hbase中数据导出(7)

    http://wiki.pentaho.com/display/BAD/Extracting+Data+from+HBase+to+Load+an+RDBMS 1)新建转换——Big Data——Hb ...

  7. HDU 2064 汉诺塔III(递归)

    题目链接 Problem Description 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘 ...

  8. intellij中javax包的导入

    新建intellij的maven web想项目的时候,出现的问题,import javax.servlet.http相关的包出错 好久不搭建也好久没用intellij了,都忘了,在这里笔记一下 再次吐 ...

  9. 【Python爬虫实战--2】时间戳转换为指定格式日期

    摘自:http://www.2cto.com/kf/201406/311477.html (1)方法: 方法一: 利用localtime()转换为时间数组,然后格式化为需要的格式,如 timeStam ...

  10. thinkPHP框架学习笔记

    class ZhuantiAction extends Action { public function index() { $name = trim($this->_get('name')); ...