Block传值

来源:互联网 发布:知轻重明得失的意思 编辑:程序博客网 时间:2024/04/30 10:51
//
//  MyViewController.m
//  Block
//
//  Created by cfy on 14-11-16.
//
//

#import "MyViewController.h"
#import "SecondViewController.h"

@interface MyViewController ()
{
    UILabel * _label;
}
@end
@implementation MyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)dealloc
{
    [_label release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.title = @"第一页";
//    self.view.userInteractionEnabled = YES;
    UIButton * butn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    butn.frame  = CGRectMake(50, 150, 100, 20);
    butn.backgroundColor = [UIColor orangeColor];
    [butn setTitle:@"跳转" forState:UIControlStateNormal];
    [butn addTarget:self action:@selector(pushPage) forControlEvents:UIControlEventTouchUpInside];
    [butn addTarget:self action:@selector(pushPage) forControlEvents:UIControlEventTouchUpInside];
    butn.userInteractionEnabled = YES;
    [self.view addSubview:butn];
    
    _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
    _label.backgroundColor = [UIColor greenColor];
    _label.text = @"fsadfadf";
    [self.view addSubview:_label];
    
    
}

-(void)pushPage
{
    SecondViewController * seVc = [[SecondViewController alloc]init];
    seVc.blocks = ^(NSString * str){
        _label.text = str;
    };
    [self.navigationController pushViewController:seVc animated:YES];
    [seVc 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




//  SecondViewController.h
//  Block
//
//  Created by cfy on 14-11-16.
//
//

#import <UIKit/UIKit.h>
typedef void(^block) (NSString * str);
@interface SecondViewController : UIViewController
@property(nonatomic,copy)block blocks;
@end


//  SecondViewController.m
//  Block
//
//  Created by cfy on 14-11-16.
//
//

#import "SecondViewController.h"
#import "MyViewController.h"
@interface SecondViewController ()
{
    UITextField * _textField;
}
@end


@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)dealloc
{
    [_textField release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"第二页";
    UIButton * butn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    butn.frame = CGRectMake(100, 120, 50, 50);
    butn.backgroundColor  = [UIColor redColor];
    [butn setTitle:@"返回" forState:UIControlStateNormal];
    [butn addTarget:self action:@selector(backPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butn];
    
    //创建一个输入框
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
    
}

-(void)backPage
{
    _blocks(_textField.text);
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (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


0 0
原创粉丝点击