block傳值
其實block傳值個人感覺跟代理很相似.也是從後往前傳.
//流程:
1.後一個界面定義一個block,並且定義一個屬性block
2.在後一個界面返回前一個界面的瞬間,(即:創建完成一個界面之後),調用block;
3.前一個界面實現block的實現
4.後一個界面在合適的機會, 讓(傳的值以參數的形式 含在block的參數裏)
代碼如下:
[objc] view plaincopy
<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
[objc] view plaincopy
<pre name="code" class="objc">#import <UIKit/UIKit.h>
typedef void(^block)(NSString * str) ;
@interface SecondViewController : UIViewController
@property (nonatomic,copy)block blocks;
@end
[objc] view plaincopy
#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
{
[objc] view plaincopy
//調用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