效果:viewController里面放置一个按钮和Lab,点击按钮进入oneViewController(delegate回调)或者BlockViewController(block回调),两者控制器里面分别有一个输入框,输入文字后 点击完成,把文本框输入的内容回调到  VIewController里面的 Lab上显示出来!

第一种:delegate回调

1.首先在oneViewController.h里面声明代理

#import <UIKit/UIKit.h>

//协议

@protocol OneViewDelegate

//代理方法

-(void)returnFieldText:(NSString*)string;

@end

@interface OneViewController : UIViewController

//声明属性

@property(nonatomic,strong)id <OneViewDelegate>delegate;

@end

2.实现代理

#import "OneViewController.h"

@interface OneViewController ()

@property(nonatomic,strong)UITextField *textField;

@end

@implementation OneViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

self.textField.backgroundColor = [UIColor greenColor];

[self.view addSubview:self.textField];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doRightButtonAction)];

self.navigationItem.rightBarButtonItem = rightButton;

}

-(void)doRightButtonAction{

NSLog(@"这是第二步");

[self.delegate returnFieldText:self.textField.text];

[self.navigationController popViewControllerAnimated:YES];

}

3. 在ViewController回调

#import "ViewController.h"

#import "OneViewController.h"

@interface ViewController ()<BlockViewDelegate>

@property(nonatomic,strong)UILabel *lab;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

self.lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 80, 200, 50)];

self.lab.backgroundColor=[UIColor redColor];

[self.view addSubview:self.lab];

}

-(void)back{

OneViewController *one=[[OneViewController alloc]init];

one.delegate = self; (注意:控制器入栈,这个必须写)

[self.navigationController pushViewController:one animated:YES];

}

//代理回调

-(void)returnFieldText:(NSString *)string{

NSLog(@"这是第3");

[self.lab setText:string];

}

第二种 Block回调

1.声明block

#import <UIKit/UIKit.h>

//声明别名ReturnTextWithBlock

typedef void (^ReturnTextWithBlock)(NSString *text);

@interface BlockViewController : UIViewController

//属性

(注意:block修饰必须用copy,不然会出问题)

@property(nonatomic,copy)ReturnTextWithBlock  returnTextBlock;

//方法

-(void)returnTextFieldWithBlock:(ReturnTextWithBlock)block;

@end

2.实现ReturnTextWithBlock

#import "BlockViewController.h"

@interface BlockViewController ()

@property(nonatomic,strong)UITextField *textField;

@end

@implementation BlockViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];

self.textField.backgroundColor = [UIColor greenColor];

[self.view addSubview:self.textField];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doRightButtonAction)];

self.navigationItem.rightBarButtonItem = rightButton;

}

-(void)doRightButtonAction{

NSLog(@"第2");

self.returnTextBlock(self.textField.text);

[self.navigationController popViewControllerAnimated:YES];

}

-(void)returnTextFieldWithBlock:(ReturnTextWithBlock)block{

NSLog(@"第1");

block是存在栈区的,随着viewDidLoad消失会消失,我们要操作 将它放入堆区,赋值  引用计数加1 ,不会导致中途丢失

self.returnTextBlock = block;

}

3.回调实现block

#import "ViewController.h"

#import "BlockViewController.h"

@interface ViewController ()

@property(nonatomic,strong)UILabel *lab;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor=[UIColor whiteColor];

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

btn.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];

self.lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 80, 200, 50)];

self.lab.backgroundColor=[UIColor redColor];

[self.view addSubview:self.lab];

}

-(void)back{

BlockViewController *BlockVC=[[BlockViewController alloc]init];

__weak typeof (self)weakSelf = self;

//回调block

[BlockVC returnTextFieldWithBlock:^(NSString *text) {

[weakSelf.lab setText:text];

}];

[self.navigationController pushViewController:BlockVC animated:YES];

}

@end

传值的话:还有属性,通知等传值方式

63.delegate回调 和block回调的更多相关文章

  1. Block回调

    •Block的定义   •Block.委托.通知.回调函数,它们虽然名字不一样,但是原理都一样,都是"回调机制"的思想的具体实现 •前面的代理模式的项目改为Block回调实现    ...

  2. iOS 键盘添加完成按钮,delegate和block回调

    这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...

  3. 代码块(Block)回调一般阐述

    本章教程主要对代码块回调模式进行讲解,已经分析其他回调的各种优缺点和适合的使用场景. 代码块机制 Block变量类型 Block代码封装及调用 Block变量对普通变量作用域的影响 Block回调接口 ...

  4. iOS开发-Block回调

    关于Block之前有一篇文章已经写过一篇文章Object-C-代码块Block回顾,不过写的比较浅显,不能体现出Block在实际开发中的重要性,关于Block的基础知识,可以参考之前的博客.在实际开发 ...

  5. Objective-C中的Block回调模式

    在前面的博客中提到了Block的概念和使用方法,个人感觉Block最爽的用法莫过于在回调时用block.感觉比委托回调和目标方法回调用着要顺手,好不好用还得读者亲自用一下才知道.如果 读者之前用过SS ...

  6. UIAlertController——之Block回调

    iOS8.0之后出现的提示框 =.=,比自己去改block回调要好.

  7. block回调具体例子

    //main.m ////  main.m//  回调////  Created by hehe on 15/9/10.//  Copyright (c) 2015年 wang.hehe. All r ...

  8. 第十篇、自定义UIBarButtonItem和UIButton block回调

    // 自定义导航栏左边按钮 self.navigationItem.leftBarButtonItem = [JQBlockedBarButtonItem blockedBarButtonItemWi ...

  9. iOS很重要的 block回调

    刚刚进入ios开发行业,发现开发中要用到大量的block回调,由此可见它的重要性.学习它之前我也是网上找的资料,推荐这篇文章http://blog.csdn.net/mobanchengshuang/ ...

随机推荐

  1. pta l2-16(愿天下有情人都是失散多年的兄妹)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 题意:两个异性的人五服之内不得通婚 ...

  2. 牛客网练习赛12---A and B

    A题传送门:https://www.nowcoder.net/acm/contest/68/A B题传送门:   https://www.nowcoder.net/acm/contest/68/B A ...

  3. 如何成功再次安装MYSQL

    以前安过,后来再安装就是停在启动项就是过不去,无响应 弄了两天,期待奇迹,网上各种教程试了个遍就是不行,大体就是删除INI,清理注册表,以下是新的发现:(转载) 如果你的电脑里装过MySQL,想再重新 ...

  4. 使用mybatis-generator-core工具自动生成mybatis实体

    我们可以使用mybatis-generator-core这个工具将数据库对象转换成mybatis对象,具体步骤如下. 1.mybatis-generator-core下载 下载地址:http://do ...

  5. ifconfig 运行command not found

    vi /home/lx/.bash_profile中添加 PATH=$PATH:$HOME/bin:/sbin:/bin export PATH   linux查看服务自启动  chkconfig - ...

  6. faiss CPU版本+GPU版本安装

    faiss安装 faiss是facebook开发的有CPU版本和GPU版本的求密集向量相似性和进行密集向量聚类的库. faiss用c++编写,安装faiss需要在github上下载其c++源码并用ma ...

  7. MVC和Web API的区别

    最近几次面试时碰到过面试官提问这个问题.我一开始觉得这两个根本没有可比性,其中有一位面试官说,有不同啊,比如继承的基类不同,webapi继承的事APIController,等等. 今天我就总结一下,其 ...

  8. join 子句(C# 参考)

    参考:https://msdn.microsoft.com/zh-cn/library/vstudio/bb311040%28v=vs.110%29.aspx 使用 join 子句可以将来自不同源序列 ...

  9. 关于Vector,map等迭代器问题

    vector.erase(it):后,it自动++,一定要弄清楚,删除成功后it指向删除的下一个地址. 对于map.erase(it),返回值为NULL,而Vector是返回itorator

  10. c# 策略模式 加工厂模式-对象与行为分离

    计算器程序 策略模式是一种行为学模式.行为是同等级的算法  ,这些行为每个模式封装到一个类里 上端提供数据   ,下端提供算法 ,中间层context context  把上端的数据和算法  放到co ...