zoukankan      html  css  js  c++  java
  • Blocks实现代理传值

    一、RootViewController:

    #import "RootViewController.h"
    #import "SecondViewController.h"
    @interface RootViewController ()
    {
        UILabel *_myLabel;
       
    }
    @end
    
    @implementation RootViewController
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.title = @"第一页";
        UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
        self.navigationItem.rightBarButtonItem = item;
        
        _myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 50)];
        _myLabel.textAlignment = NSTextAlignmentCenter;
        _myLabel.text = @"Blocks";
        [self.view addSubview:_myLabel];
        // Do any additional setup after loading the view from its nib.
    }
    -(void)nextPage{
        SecondViewController *second = [[SecondViewController alloc]initWithBlock:^(NSString *str) {
            NSLog(@"%@",str);
            _myLabel.text = str;
        }];
        [self.navigationController pushViewController:second animated:YES];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    


    二、SecondViewConroller:

    .h文件

    #import <UIKit/UIKit.h>
    typedef void(^myBlock)(NSString *);
    
    @interface SecondViewController : UIViewController
    {
        myBlock block;
    }
    -(id)initWithBlock:(myBlock)str;
    @end

    .m文件

    #import "SecondViewController.h"
    
    @interface SecondViewController ()
    
    @end
    
    @implementation SecondViewController
    -(id)initWithBlock:(myBlock)str{
        self = [super init];
        if(self)
        {
            block = str;
        }
        return self;
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        myButton.frame = CGRectMake(100, 100, 100, 50);
        [myButton setTitle:@"点我传值!" forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:myButton];
        
        // Do any additional setup after loading the view from its nib.
    }
    -(void)clicked{
        NSLog(@"我被点击了!

    "); if (block) { block(@"哈哈"); } //[self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }



  • 相关阅读:
    Windows Server 2003 DHCP迁移到2008R2的方法
    在Linux上安装Zabbix agent的方法
    在Windows上安装Zabbix agent的方法
    利用pscp程序实现Windows和Linux互传文件
    CentOS 7 LNMP环境搭建 Zabbix3.4
    2019-2020-2 20175105王鑫浩《网络对抗技术》Exp9 Web安全基础
    2019-2020-2 20175105王鑫浩《网络对抗技术》Exp8 Web基础
    2019-2020-2 20175105王鑫浩《网络对抗技术》Exp7 网络欺诈防范
    2019-2020-2 20175105王鑫浩《网络对抗技术》Exp6 MSF基础应用
    2019-2020-2 20175105王鑫浩《网络对抗技术》Exp5 信息搜集与漏洞扫描
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6820884.html
Copyright © 2011-2022 走看看