ios之block代码块的简单使用 函数回调

// block 的简单使用demo  首先创建两个ViewController
#import "RootViewController.h"
#import "SencondViewController.h"
typedef void(^myBlock)(int ,UIButton*);
@interface RootViewController ()
{
    UILabel *label;
    myBlock block;
}

@end
@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, 100, 150, 50)];
    label.backgroundColor = [UIColor brownColor];
    label.font = [UIFont boldSystemFontOfSize:30];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake(0, 0, 100, 100);
    btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    [btn setTitle:@"present" forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:25]];
    [btn setBackgroundColor:[UIColor greenColor]];
    [btn addTarget:self action:@selector(presentSencondVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}
- (void)presentSencondVC
{
    SencondViewController *sencondVC = [[SencondViewController alloc]init];
    block = ^(int a,UIButton*btn)
    {
        NSLog(@"%@",btn);
        self.view.backgroundColor =[UIColor redColor];
        label.text = [NSString stringWithFormat:@"%d",a];
    };
    sencondVC.backBlock = block;
    [self presentViewController:sencondVC animated:YES completion:nil];
    [sencondVC release];
}
#import <UIKit/UIKit.h>
typedef void(^callBack)(int,UIButton*);

@interface SencondViewController : UIViewController

@property(nonatomic,copy)callBack backBlock;
@end


 
#import "SencondViewController.h"
typedef  void(^myblock)(void);
@interface SencondViewController ()
{
    myblock testBlock;
}
@end

@implementation SencondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake(0, 0, 100, 100);
    btn.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    [btn setTitle:@"dismiss" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor grayColor]];
    [btn addTarget:self action:@selector(dismissTopVC:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 100;
    [self.view addSubview:btn];
    //arc模式下,不能使用self 或者全局变量,属性
//    __weak __block SencondViewController *sencondVC = self;
    testBlock = [^{
        [self printf];
    }copy];

}
- (void)dismissTopVC:(UIButton*)button
{
    //调用block,这时rootVC的block马上机会执行
    self.backBlock(button.tag,button);

    testBlock();
    //释放block
    Block_release(testBlock);
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (void)printf
{
    NSLog(@"123");
}
- (void)dealloc
{
    NSLog(@"%s",__FUNCTION__);
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值