63.delegate回调 和block回调

Posted qiangzhevsruozhe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了63.delegate回调 和block回调相关的知识,希望对你有一定的参考价值。

效果: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回调的主要内容,如果未能解决你的问题,请参考以下文章

ios block和delegate的区别

嵌入式&iOS:回调函数(C)与block(OC)回调对比

关于block的回调使用-防止内存泄露问题

整理IOS中Block用作属性传值回调用法

嵌入式&iOS:回调函数(C)与block(OC)传 参/函数 对比

iOS:使用block代码块实现事件处理过程中的回调