blocks实现两个类之间通信

本文介绍如何在iOS开发中使用Block实现按钮点击事件及视图控制器间的回调交互,并探讨Block的内存管理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*

     block引用局部变量,会把该变量作为常量编码到block块中,所以不能修改需要有__block修饰才可以

      __block n = 5;

     block内引用局部的Objective-C对象的时候,该对象会被retain,如果局部变量使用__block修饰,则不会被retain

     block里面使用一个对象里面的变量(属性)时,该对象会被retain

     block本身可以像对象一样copyrelease

     block创建后内存在栈上(编译器管理内存),调用copy后会移到堆上(手动管理)

     block声明为全局变量时,我用需要调用blockcopy方法

     */


先看一个自定义button,点击事件用回调实现

BlockBtn.h

#import <UIKit/UIKit.h>

// 定义block

typedefvoid(^BlockTap)(UIButton *);


@interface BlockBtn :UIButton

// copy属性,把block移到堆上

@property(nonatomic,copy)BlockTap blockTap;


@end

//----------

BlockBtn.m

#import "BlockBtn.h"

@implementation BlockBtn

- (void)dealloc

{

    // 释放block

    Block_release(_blockTap);

    [superdealloc];

}

- (instancetype)init

{

   self = [superinit];

   if (self) {

        // 给自己添加点击事件

        [selfaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

    }

    return self;

}

- (void)click:(UIButton *)btn

{

    _blockTap(self);

}

下面的类中会使用这个Btn的回调

- (void)viewDidLoad {

    [superviewDidLoad];

   //self.view.translatesAutoresizingMaskIntoConstraints = NO;

    BlockBtn *btn = [[BlockBtnalloc] init];

    btn.frame = CGRectMake(0.0,0.0, 100, 100);

    //btn.translatesAutoresizingMaskIntoConstraints = NO;

    [btn setTitle:@"点击"forState:UIControlStateNormal];

    btn.backgroundColor = [UIColorredColor];

    [self.viewaddSubview:btn];

    [btn release];

//    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:30.0]];

//    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:80.0]];

//    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:60.0]];

//    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:90.0]];

    __blockViewController *vc = self;

    btn.blockTap = ^(UIButton *btn) { //btn的回调推出下一视图

        NSLog(@"one show");

        JJOneViewController *one = [[[JJOneViewControlleralloc] init] autorelease];

        [vc presentViewController:oneanimated:YEScompletion:^{}];

        one.oneBlock = ^() { //JJOneViewController消失后改变背景色

            vc.view.backgroundColor = [UIColorpurpleColor];

        };

    };

}

JJOneViewController.h

#import <UIKit/UIKit.h>

typedefvoid(^OneBlock)(void);

@interface JJOneViewController :UIViewController

@property(nonatomic,copy)OneBlock oneBlock;

@end

JJOneViewController.m

- (void)dealloc

{

    NSLog(@"JJOneViewController释放了");

    Block_release(_oneBlock);

    [superdealloc];

}

- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorgrayColor];    

//    BlockBtn *btn = [[BlockBtn alloc] init];

//    btn.frame = CGRectMake(10.0, 90.0, 70, 100);

//    btn.backgroundColor = [UIColor greenColor];

//    [self.view addSubview:btn];

//    [btn release];

//    __block JJOneViewController *one = self;

//    btn.blockTap = ^(UIButton *btn) {

//        [one dismissViewControllerAnimated:YES completion:^{

//            _oneBlock();

//        }];

//    };

   UIButton *btn = [[UIButtonalloc] init];

    btn.frame =CGRectMake(10.0,90.0, 70, 100);

    btn.backgroundColor = [UIColorgreenColor];

    [self.viewaddSubview:btn];

    [btnrelease];

    [btn addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)click:(UIButton *)btn

{

    [selfdismissViewControllerAnimated:YEScompletion:^{

       _oneBlock();

    }];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值