iOS UI 12 block传值


//

//  RootViewController.m

//  UI- 12 block传值

//

//  Created by dllo on 15/11/24.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "RootViewController.h"

#import "SencondViewController.h"

@interface RootViewController ()


@end


@implementation RootViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style: UIBarButtonItemStylePlain target:self action:@selector(rigthAct:)];

    

//    void (^bolck1)(void) = ^(void) {

//        NSLog(@"1");

//    };

//    bolck1();

    

    

    

    

    

    

    

    

    

    // Do any additional setup after loading the view.

}

- (void)rigthAct:(UIBarButtonItem *)sender

{

//    SencondViewController *SendVc = [[SencondViewController alloc]init];

    

//    __unsafe_unretained RootViewController *rootvc = self;

//    [self.navigationController pushViewController:SendVc animated:YES];

//    void (^change)(UIColor *) = ^(UIColor *mycolor) {

        self.view.backgroundColor = mycolor;

//        rootvc.view.backgroundColor = mycolor;

//    };

//    [SendVc sendblock:change str:@"aaa"];

    

    //简写

#warning block传值1 - 定义block 并通过调用sendVC的方法将block的地址传过去

    SencondViewController *SendVc = [[SencondViewController alloc]init];


    __unsafe_unretained RootViewController *rootvc = self;

    [SendVc sendblock:^(UIColor *mycolor) {

        self.view.backgroundColor = mycolor;

        

    } str:@"aaa"];

    [self.navigationController pushViewController:SendVc animated:YES];

    [SendVc release];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

//

//  SencondViewController.h

//  UI- 12 block传值

//

//  Created by dllo on 15/11/24.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <UIKit/UIKit.h>

typedef void (^blackType)(UIColor *);

@interface SencondViewController : UIViewController


{

    

    //    void (^_myblock)(UIColor *);

#warning block传值3 定义实例变量 ,指向传递来的block,可供其他方法内调用block

    blackType _myBlock;

}

#warning block传值2  - 声明方法 用来接受block的地址 同时可以顺带接受其他传递信息

- (void)sendblock:(blackType)bolck str:(NSString *)str;

@end


//

//  SencondViewController.m

//  UI- 12 block传值

//

//  Created by dllo on 15/11/24.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "SencondViewController.h"






@implementation SencondViewController



- (void)dealloc

{

#warning block传值6 - 引用计数减1

    Block_release(_myBlock);

    [super dealloc];

    

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    UIButton  *button  = [ UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(50, 100, 50, 50);

    button.backgroundColor = [UIColor purpleColor];

    [button addTarget:self action:@selector(buttonAct:) forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"发春" forState:UIControlStateNormal];

    [self.view addSubview:button];

    

    

    

    // Do any additional setup after loading the view.

}

- (void)buttonAct:(UIButton *)sender

{

#warning block传值5  - 调用block 传递参数

    _myBlock([UIColor redColor]);

}

- (void)sendblock:(blackType)bolck str:(NSString *)str;

{

    NSLog(@"%@",str);

//    _myblock = bolck;

//    bolck([UIColor yellowColor]);

#warning block传值4  - rootVC中定义的block空间拷贝到堆区,防止释放

       _myBlock = Block_copy(bolck);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值