• 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




  • 相关阅读:
    UDP通讯demo
    Git 修改已提交的commit注释
    git merge和git merge --no-ff的区别
    git常用命令
    阿里云域名证书申请流程
    IntelliJ IDEA常用快捷键总结
    宿主CentOS7环境下docker中rabbitMQ的安装
    宿主CentOS7环境下docker中nginx的安装
    docker常用命令
    CentOS7环境下docker安装
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043079.html
Copyright © 2020-2023  润新知