Swift的函数用法还真是灵活.但是个人感觉更灵活的还是闭包.

swift闭包的概念大抵相当于OC的block的概念.如果对于block的理解很透彻的话,闭包的原理性的东西还是很好理解的.

剩下的就是灵活多变的用法了.在学习闭包之前,我还是想从新再总结一下block的原理和用法.毕竟闭包用好了真是简化了好多东西.

block

block的基本概念与定义

block的基本定义: 返回值 (^block名字)(形参类型) = ^(形参类型 形参名){ 执行代码块 }

int (^myblock)(int) = ^(int num){

        NSLog(@"block");

        return num*;

    };

    //调用  myblock(实参)

   int i =  myblock();

    NSLog(@"%d",i);

无返回值 无参数的block

void (^block1)() = ^(){

        NSLog(@"无返回值 无参数的block");

    };

    //调用

    block1();

有返回值 无参数

int (^block2)() = ^(){
return ; }; block2();

无返回值 有参数

 void (^block3)(int) = ^(int num){

        NSLog(@"block3 = %d",num);
}; block3();

外部参数如果在block 内部使用 需要加上__block

    __block int y = ;

    int (^block)(int) = ^(int num){

        NSLog(@"block");

        return num + y;

    };

   int bl =  block();

    NSLog(@"%d",bl);

block的传值

block的传值一般用于回调.假如有A和B两个控制器.Apush到B.那么想把B控制器其中的一个值传到A控制器里面使用就可以使用block. -->从后往前传

1.定义->在B控制器中定义 block参数.将block作为参数回调给A

typedef void(^myBlock)(NSString *name);
@interface oneViewController : UIViewController @property (nonatomic,copy) myBlock myBlock;
@end

2.调用->在B控制器需要传值的地方,调用定义好的 block

    if (self.myBlock) {
self.myBlock(self.textfield.text);
}

3.实现->在A控制器中 实现 block

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

    one.myBlock = ^(NSString *str) {

        self.textfield.text = str;
};

附上完整代码

A控制器的.m

#import "ViewController.h"
#import "oneViewController.h"
@interface ViewController () @property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UITextField *textfield; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor redColor]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(, , ,)];
btn.backgroundColor = [UIColor whiteColor];
[self.view addSubview:btn];
self.btn = btn;

[btn setTitle:@"one" forState:UIControlStateNormal];

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

self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];

self.textfield.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:self.textfield]; }

//对应上面的 第三步 ->实现
- (void)jump:(UIButton *)btn{ oneViewController *one = [[oneViewController alloc]init]; one.myBlock = ^(NSString *str) { self.textfield.text = str;
}; [self.navigationController pushViewController:one animated:YES]; }

B控制器的.h

//对应上面的第一步 ->定义
#import <UIKit/UIKit.h> typedef void(^myBlock)(NSString *name);
@interface oneViewController : UIViewController @property (nonatomic,copy) myBlock myBlock; @end

B控制器的.m

#import "oneViewController.h"

@interface oneViewController ()

@property (nonatomic,strong) UIButton *btn;
@property (nonatomic,strong) UITextField *textfield; @end @implementation oneViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(, , ,)];
btn.backgroundColor = [UIColor whiteColor];
[self.view addSubview:btn];
self.btn = btn;

[btn setTitle:@"one" forState:UIControlStateNormal];

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

    self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
self.textfield.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:self.textfield];
}
//对应上面的第二步 ->调用
-(void)jump:(UIButton *)btn { if (self.myBlock) {
self.myBlock(self.textfield.text);
} [self.navigationController popToRootViewControllerAnimated:YES];
} @end

Swift 05.Block的更多相关文章

  1. swift中block的使用

    在OC中习惯用block来传值,而swift中,block被重新定义了一下,叫闭包: 使用的技巧:谁定义谁传值:   案例使用A.B控制器: 1~4步在B中执行,最后在A中执行: - B控制器: 1- ...

  2. Swift - 05 - 数值型字面量

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  3. JSPatch 遇上swift

    swift使用JSPatch要点: 继承自NSObject的Swift类,其继承自父类的方法具有动态性,其他自定义方法.属性需要加dynamic修饰才可以获得动态性(public属性除外) 例子可参看 ...

  4. Swift与JS的交互

    Swift与JS的交互 原理 同Object-C与JS交互的大同小异,只是方法形式改变了. 首先我们需要引入iOS7.0出来的JavaScriptCore.framework JavaScriptCo ...

  5. NSObject+YYModel.h

    01 - nil Nil NULL NSNull kCFNull  的区别 参考:http://www.jianshu.com/p/3aaefb3bcf73 02 - __bridge,__bridg ...

  6. openstack是什么

    下面图片多来自互联网. 云计算: 云计算层次机构模型: IaaS 基础设施服务 PaaS 平台级服务 SaaS 软件级服务 官网:http://www.openstack.org/ openstack ...

  7. 红帽 Enterprise Linux OpenStack Platform 4.0全面上市

    十一月,红帽公司推出Red Hat Enterprise Linux OpenStack Platform 4.0测试版,这款企业级解决方案集Red Hat Enterprise Linux的稳定性与 ...

  8. OpenStack项目列表

    这个也是必须要熟悉的哟. ~~~~~~~~~~ OpenStack是一个美国国家航空航天局和Rackspace合作研发的,以Apache许可证授权,并且是一个自由软件和开放源代码项目.OpenStac ...

  9. OC加强-day03

    #program mark - 0_18 分类的使用注意 [掌握] 1.分类的作用 作用:讲一个类分为多个模块,将相似功能的方法写在同一个模块中,方便我们后面代码的维护 "强调 1.分类中只 ...

随机推荐

  1. (9) 深入了解Java Class文件格式(八)

    转载:http://blog.csdn.net/zhangjg_blog/article/details/22205831 在本专栏的第一篇文章 深入理解Java虚拟机到底是什么 中, 我们主要讲解了 ...

  2. mariadb:InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes

    mariadb 启动中 InnoDB: Error: log file ./ib_logfile0 is of different size 0 起因:线上正在运行的系统,因为需要调整性能,变更了my ...

  3. 【线段树区间合并】HDU1540-Tunnel Warfare

    一.题目 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  4. sqlalchemy中文乱码问题解决方案

    本文参考http://firefish.blog.51cto.com/298258/112794/的解决方案 问题: 本文在Ubuntu上利用scrapy抓取数据写入mysql数据库时,用到sqlal ...

  5. 对象-3.py

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #cbcbcb } p.p2 { margin: 0.0px 0 ...

  6. 什么是hasLayout?

    想更好的理解CSS,尤其是IE下对CSS的渲染,hasLayout是一个非常有必要彻底弄清楚的概念,大多数IE下的显示错误,就是源于hasLayout.hasLayout是一种只读属性,有两种状态tr ...

  7. 关于APP程序员泡沫经济

    这些年,移动互联网非常火,火到掀起学习iOS.安卓以及H5的热潮.有人将这些新技术作为自己的实力补充,增加竞争力:更多的人将它们作为主业,专职做移动开发.但是,即便有移动开发人员不断涌入,对整个行业来 ...

  8. linux @后面的主机名如何修改

    @后面的为linux系统的主机名 临时修改方法:执行 hostname 主机名再执行 bash 永久修改方法:修改配置文件/etc/sysconfig/network修改参数HOSTNAME=主机名永 ...

  9. Write a script to check an interesting game 6174

    # -*- coding: utf-8 -*-#from ftplib import FTPimport osdef sort_reverse(x,y): if x>y: return -1 i ...

  10. html5 formData上传 针对app端

    function uploadFile() { if ((document.getElementById("file").files[0].size / 1024).toFixed ...