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. openstack私有云布署实践【7.2 keystone + memcache (办公网环境)】

    首先登录controller1创建keystone数据库,并赋于远程和本地访问的权限.   mysql -u root -p CREATE DATABASE keystone; GRANT ALL P ...

  2. Java、C#双语版配套AES加解密示例

      这年头找个正经能用的东西那是真难,网上一搜索一大堆,正经能用的没几个,得,最后还是得靠自己,正巧遇上需要AES加解密的地方了,而且还是Java和C#间的相互加解密操作,这里做个备忘 这里采用的加解 ...

  3. awstats 日志分析

    /tmp/awstats/awstats.ezrydel.com.conf LogFile="/usr/local/apache/domlogs/ezrydel.com" php版 ...

  4. ubutun下安装jenkins

    安装方法所在网址:   https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Ubuntu 在安装jenkins之前,ub ...

  5. netty初探(1)

    参考目录: 1. user-guide : http://netty.io/wiki/user-guide-for-4.x.html 2. demo: http://netty.io/wiki/ 3. ...

  6. spring源码

    今天看了看spring对于视图解析的源码,发现还不是那些思想,internalResourceView里的一个渲染方法 protected void exposeModelAsRequestAttri ...

  7. NOIP2010-普及组复赛-第一题-数字统计

    题目描述 Description 请统计某个给定范围[L, R]的所有整数中,数字 2 出现的次数. 比如给定范围[2, 22],数字 2 在数 2 中出现了 1 次,在数 12 中出现 1 次,在数 ...

  8. a标签包含块级元素问题

    a标签包含块级元素是不符合W3c标准的,但是淘宝也有这样的布局暂且认为可以这样(有时候布局需要这样写) 当a标签包含了div这样的块级元素时a标签是要转换成块级元素的使用display:block.但 ...

  9. perl Socket接收超时设置

    一般来说, IO::Socket::INET里的Timeout设置是对于conncet的 如果你想设置recv接收超时, 可以这样设置: usr Socket: ...... , )); #注意这里p ...

  10. php 分页类(1)

    inter.php <head> <meta http-equiv="Content-Type" content="text/html; charset ...