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. win10桌面和手机的扩展API,判断是否有实体后退键API

    喜大普奔的win10 uap开发预览版终于出了,这次更新跟8.1的变化不是很大,但是将原本win8.1和wp8.1uap的分项目的形式,改为了整合成一个项目,经过一次编译打包成一个appx包,实现了无 ...

  2. (sql占位符)Remember that ordinal parameters are 1-based!

    第一次出现是 将第一个问号(?)的位置设为1了,其实是从0开始的. 第二次出现是 sql中的 问号 写成了 中文输入法下的问号,这个太难发现了,搞了半天才发现.然后改成英式问号就可以了. 每天进步一点 ...

  3. ggplot2 geom相关设置—分布图

    分布在R中应该算是个比较重要的内容,而通过画图来展示数据的分布,可以更直观的让我们了解数据的分布情况 直方图 geom_histogram(mapping = NULL, data = NULL, s ...

  4. Java 多态 父类和子类方法的访问控制权限

    Java 多态 父类和子类方法的访问控制权限 @author ixenos 父类和子类方法的访问控制权限 继承是为了扩展类的功能,而这种扩展显然就是对一个原始类的扩展,目的还是向上转型来调用,所以这就 ...

  5. Java 集合 JDK1.7的LinkedList

    Java 集合 JDK1.7的LinkedList @author ixenos LinkedList LinkedList是List接口的双向链表实现,JDK1.7以前是双向循环链表,以后是双向非循 ...

  6. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  7. boost 编译依赖库

    正则表达式 icu-devel python python-devel bzip2-devel ./b2 variant=release link=shared threading=multi run ...

  8. Gentoo安装详解(一) -- 安装基本系统

    前期准备 远程登录: 开启ssh服务: /etc/init.d/sshd start 设置密码: passwd 以便使用putty.ssh client远程登录上传stage等(有时在线下载很慢,而局 ...

  9. 洛谷-生活大爆炸版石头剪刀布-NOIP2014提高组复赛

     题目描述 Description 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的升级版 ...

  10. 循环移位法和数据拼接法基于led

    功能描述 让led每隔0.5s从两边向中间闪烁,然后在从中间向两边闪烁,不断循环 项目实现 开发板 晶振为50M,那么达到0.5s时计数器count1需要达到24_999_999这么多次数 计数器代码 ...