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. 【第七篇】Volley之处理Gzip数据

    一般对于API请求需带上GZip压缩,因为API返回数据大都是Json串之类字符串,GZip压缩后内容大小大幅降低. public class GZipRequest extends StringRe ...

  2. 重载 vs 重写

    http://www.cnblogs.com/lonelyDog/archive/2011/11/16/2251011.html

  3. 永久修改python默认的字符编码为utf-8

    这个修改说来简单,其实不同的系统,修改起来还真不一样.下面来罗列下3中情况 首先所有修改的动作都是要创建一个叫 sitecustomize.py的文件,为什么要创建这个文件呢,是因为python在启动 ...

  4. 常用的HTTP状态码如下

    成功的状态码: – 服务器成功返回网页 – 未修改 失败的状态码: – 请求的网页不存在 – 服务器暂时不可用 – 服务器内部错误 下面的不是很常用,记住上面那几个就ok了,有bug了再补充 其他的状 ...

  5. [JS高程]引用类型(Object、Array)

    引用类型:Object.Array Object: person.name   =>推荐,除非必须使用变量([])来表示 person["name"] 区别:[]可以通过变量 ...

  6. @Transactional注解详解

    默认遇到throw new RuntimeException("...");会回滚   需要捕获的throw new Exception("...");不会回滚 ...

  7. Java 集合 LinkedList的ListIterator

    Java 集合 LinkedList的ListIterator @author ixenos 摘要:ListIterator<E>是继承自Iterator<E>的接口.list ...

  8. 使用NGUI实现拖拽功能(拼图小游戏)

    上一次用UGUI实现了拼图小游戏,这次,我们来用NGUI来实现 实现原理 NGUI中提供了拖拽的基类UIDragDropItem,所以我们要做的就是在要拖拽的图片上加一个继承于该类的脚本,并实现其中的 ...

  9. JVM问题诊断常用命令:jinfo,jmap,jstack

    1.jinfo 描述:输出给定 java 进程所有的配置信息.包括 java 系统属性和 jvm 命令行标记等. 用法: jinfo [ option ] pid jinfo [ option ] e ...

  10. Java Socket与操作系统的关系

    Socket与操作系统有什么关系呢?请细读下文 简介 TCP简介 TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议 ...