码迷,mamicode.com
首页 > 其他好文 > 详细

Blocks实现代理传值

时间:2014-06-19 10:24:12      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:blocks   代理   传值   闭包   ios   

一、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.
}


Blocks实现代理传值,布布扣,bubuko.com

Blocks实现代理传值

标签:blocks   代理   传值   闭包   ios   

原文地址:http://blog.csdn.net/zhiwei_qin/article/details/30047679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!