iOS 视图之间传值(二)Block传值

实现从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传值完整代码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值