zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    洛谷P1129 [ZJOI2007] 矩阵游戏(二分图最大匹配)
    牛客NC51316 Going Home (二分图最大权匹配)
    洛谷P2055 [ZJOI2009]假期的宿舍(二分图最大匹配)
    Codeforces Round #702 (Div. 3) 全部七题
    Educational Codeforces Round 104 (Rated for Div. 2) A~D
    Leetcode 567. 字符串的排列(滑动窗口)
    基于macOS Catalina 10.15.7下GitHub Pages + Hexo 5.3.0 + 阿里云的博客部署
    关于两个数的LCM
    2021牛客寒假算法基础集训营1 补题 ABCEFIJ
    macOS上运行jupyter notebook程序:服务似乎挂掉了,但是会立刻重启 报错OMP: Error #15: Initializing libomp.dylib, but found libiomp5.dylib already initialize
  • 原文地址:https://www.cnblogs.com/qiangzheVSruozhe/p/9435556.html
Copyright © 2011-2022 走看看