实现从secondViewController中的视图向viewController中的视图传值
在secondViewController创建block
#import <UIKit/UIKit.h>
#import "UserEntity.h"
typedef void(^Block)(UserEntity* userEntity);
@interface SecondViewController : UIViewController
{
Block block;
}
@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;
-(void)setBlock:(Block)myBlock;
- (IBAction)okBtnClicked:(id)sender;
- (IBAction)closeKeyboard:(id)sender;
@end
@implementation SecondViewController
@synthesize nameTextField;
@synthesize ageTextFiled;
@synthesize gendarTextField;
-(void)setBlock:(Block)myBlock
{
if (block!=myBlock) {
Block_release(block);
block=Block_copy(myBlock);
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"Second";
}
- (void)viewDidUnload
{
[self setNameTextField:nil];
[self setAgeTextFiled:nil];
[self setGendarTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[nameTextField release];
[ageTextFiled release];
[gendarTextField release];
Block_release(block);
[super dealloc];
}
- (IBAction)okBtnClicked:(id)sender {
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.nameTextField.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextFiled.text intValue];
if (block)
{
block(userEntity);
}
//退回到第一个窗口
[self.navigationController popViewControllerAnimated:YES];
[userEntity release];
}
/*单击屏幕其他区域关闭键盘的方法
实现方法是:首先选中xib文件的view,设置class为UIControl
然后在事件中选择Touch Down拖线到.h文件中声明该方法,最后实现下面即可
*/
- (IBAction)closeKeyboard:(id)sender {
[self.nameTextField resignFirstResponder];
[self.ageTextFiled resignFirstResponder];
[self.gendarTextField resignFirstResponder];
}
@end
在viewController代码块实现,传递参数
#import <UIKit/UIKit.h>
#import "UserEntity.h"
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@property (retain, nonatomic) IBOutlet UILabel *ageLabel;
@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;
- (IBAction)openBtnClicked:(id)sender;
@end
@implementation ViewController
@synthesize nameLabel;
@synthesize ageLabel;
@synthesize gendarLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = @"First";
}
- (void)viewDidUnload
{
[self setNameLabel:nil];
[self setAgeLabel:nil];
[self setGendarLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[nameLabel release];
[ageLabel release];
[gendarLabel release];
[super dealloc];
}
//点击进入第二个窗口的方法
- (IBAction)openBtnClicked:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
/*实现代码块*/
[secondView setBlock:^(UserEntity *value) {
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age];
self.gendarLabel.text = value.gendar;
}];
/*切换到第二个页面*/
[self.navigationController pushViewController:secondView animated:YES];
[secondView release];
}
@end
完整代码下载: Block传值完整代码下载
本文介绍如何在iOS应用中通过Block在不同视图控制器之间传递数据,包括在secondViewController中创建Block,以及如何在viewController中利用Block接收并处理数据。
415

被折叠的 条评论
为什么被折叠?



