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

block传值

时间:2014-07-19 23:11:19      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:控件   对象   界面   导航   uikit   

bubuko.com,布布扣

                              block传值

其实block传值个人感觉跟代理很相似.也是从后往前传.
//流程:
1.后一个界面定义一个block,并且定义一个属性block
2.在后一个界面返回前一个界面的瞬间,(即:创建完成一个界面之后),调用block;
3.前一个界面实现block的实现
4.后一个界面在合适的机会, 让(传的值以参数的形式 含在block的参数里)

代码如下:


<pre name="code" class="objc">#import "FirstViewController.h"
#import "SecondViewController.h"
#import "UIButton+Create.h"
@interface FirstViewController ()
{
    UILabel * _label;
}
@end

@implementation FirstViewController
- (void)dealloc
{
    [_label release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"首页";
    self.view.userInteractionEnabled = YES;
    /**
     *  1.创建一个UIButton,
     *  2.并添加响应事件,从首页跳转到第二个页面.
     */
    UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
    button.userInteractionEnabled = YES;
    [self.view addSubview:button];
    
    
    
    /**
     *  1.在第1个界面创建一个UILabel
     *  2.把第二页输入框输入的字符串,通过block内部实现传过来
     *  3.然后通过赋值给UILabel
     */
    _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
    _label.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_label];
    
	// Do any additional setup after loading the view.
}

- (void)didClickButtonAction
{
    
    /**
     *  1.用push的方法推出下一个页面
     *  2.把第二页输入框输入的字符串,通过block内部实现传过来
     *  3.从而实现把输入框输入的字符串,传到UILabel上.
     */
    SecondViewController * secondVC = [[SecondViewController alloc]init];
    secondVC.blocks=^(NSString * str){
        
         _label.text = str;
    };
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end



<pre name="code" class="objc">#import <UIKit/UIKit.h>

typedef void(^block)(NSString * str) ;
@interface SecondViewController : UIViewController
@property (nonatomic,copy)block blocks;
@end



#import "SecondViewController.h"
#import "UIButton+Create.h"
#import "FirstViewController.h"
@interface SecondViewController ()
{
    UITextField * _textField;//创建一个输入框
}
@end
@implementation SecondViewController

- (void)dealloc
{
    [_textField release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"第二页";
    /**
     *  1.创建一个UIButton,
     *  2.并添加响应事件,从第二个页面返回到首页.
     */
    UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
    [self.view addSubview:button];
    
    /**
     *  1.在第二个界面创建一个输入框
     *
     */
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
    
    
    
	// Do any additional setup after loading the view.
}

- (void)didClickButtonAction
{
   //调用block方法,把<span style="font-family: Arial;">_textField.text作为参数传过去</span>

    _blocks(_textField.text);
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


bubuko.com,布布扣

bubuko.com,布布扣




block传值

标签:控件   对象   界面   导航   uikit   

原文地址:http://blog.csdn.net/zuoyou1314/article/details/37964321

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