设为首页 加入收藏

TOP

Swift 05.Block(一)
2017-10-10 12:17:16 】 浏览:2110
Tags:Swift 05.Block

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

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

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

block

block的基本概念与定义

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

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

        NSLog(@"block");

        return num*2;

    };

    //调用  myblock(实参)

   int i =  myblock(3);

    NSLog(@"%d",i);

无返回值 无参数的block

    

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

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

    };

    //调用

    block1();

有返回值 无参数

    

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

    };

    block2();

无返回值 有参数

   

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

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

    block3(5);

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

    __block int y = 90;

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

        NSLog(@"block");

        return num + y;

    };

   int bl =  block(10);

    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(150, 150, 20,30)];
    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(50, 50, 200, 30)];

     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 viewD
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇窥探Swift之使用Web浏览器编译Swi.. 下一篇窥探Swift系列博客说明及其Swift..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目