/// FirstViewController - (void)viewDidLoad { [super viewDidLoad];
self.nameLable = [[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 60)]autorelease]; self.nameLable.textAlignment = UITextAlignmentCenter; self.nameLable.font = [UIFont systemFontOfSize:50]; self.nameLable.textColor = [UIColor blueColor]; [self.view addSubview:self.nameLable];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(130, 170, 60, 40); [button setTitle:@"下一个" forState:UIControlStateNormal]; [button addTarget:self action:@selector(pushNext:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; }
- (void)pushNext:(id)sender { //初始化second SecondViewController *second = [[SecondViewController alloc]init]; ///调用块 second.send = ^(NSString *str){ self.nameLable.text = str; }; //推过去 [self.navigationController pushViewController:second animated:YES]; [second release]; } |
/
SecondViewController.h
#import <UIKit/UIKit.h>
typedef void (^SendMessage) (NSString *str); ///声明块
@interface SecondViewController : UIViewController<UITextFieldDelegate>
@property (nonatomic, copy) SendMessage send; //声明一个块类型属性
@end
/SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UITextField *textFd = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 300, 150)];
textFd.borderStyle = UITextBorderStyleRoundedRect;
textFd.delegate = self;
textFd.tag = 100;
[self.view addSubview:textFd];
[textFd release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
//先判断,在调用块传递实参
if (self.send) {
self.send (textField.text);
}
return YES;
}