简单的block页面传值



#import <UIKit/UIKit.h>

// 1⃣️ 定义 block 类型
typedef void (^BlockType)( NSString *);
//typedef <#returnType#>(^<#name#>)(<#arguments#>);
@interface BBBViewController : UIViewController

/** 内容 */
@property ( nonatomic , copy ) NSString *text;

/** 2⃣️ 定义 block 属性 */
@property ( nonatomic , copy ) BlockType textBlock;


// 3⃣️ 实现有 block 参数的方法
- (
void )setTextBlock:( BlockType )textBlock;

@end



#import "BBBViewController.h"

@interface BBBViewController ()

@property ( weak , nonatomic ) IBOutlet UITextField *tf;

@end

@implementation BBBViewController
- (
void )viewWillAppear:( BOOL )animated{

    [
super viewWillAppear :animated];
   
   
self . tf . text = self . text ;
   
}

- (
IBAction )dismiss:( id )sender {
   
   
// 调用代理方法 :
//    [_delegate 代理方法 :_tf.text];
   
   
// 调用 block:
   
_textBlock ( _tf . text );
   
    [
self dismissViewControllerAnimated : YES completion :^{
       
    }];
}




- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
// Do any additional setup after loading the view from its nib.
}



@end





//
//  ViewController.m
//  02 页面传值 -block
//
//  Created by CORYIL on 16/5/3.
//  Copyright © 2016 徐锐 . All rights reserved.
//

#import "ViewController.h"
#import
"BBBViewController.h"
#import
<objc/runtime.h>
@interface ViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *tf;

@end

@implementation ViewController
+(
void )initialize{
   
   
unsigned int count = 0 ;
    Ivar*ivars = class_copyIvarList([UIButton class], &count);
   
for ( int i = 0 ; i<count; i++) {
        Ivar ivar = *(ivars+i);
        printf(
"%s\n" ,ivar_getName(ivar));
    }
   
   
}

- (
IBAction )present:( id )sender {
   
   
   
/**
     block
流程 :
    
       
本类为 A   弹出的控制器为 B
    
        1.
声明 : 类型 + 名字
        2.
赋值 : 实现 block     A
        3.
调用 : 执行 block     B
     */

   
   
BBBViewController *bbb = [[ BBBViewController alloc ] init ];
   
    bbb.
text = self . tf . text ;
   
    [bbb
setTextBlock :^( NSString *text) {
      
       
self . tf . text = text;
    }];
   
   
//    bbb.delegate = self;
   
    [
self presentViewController :bbb animated : YES completion :^{
       
    }];
   
}


// 代理的方法的实现 :

//- (void) 代理方法 :(NSString *)text{
//
//    self.tf.text = text;
//}

block循环引用解决办法
__ weak   typeof ( self ) weakSelf =  self ;
__ strong   typeof ( self ) strongSelf = weakSelf;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值