第一个view的代码
// // FirstViewController.h // ValuePassByView // // Created by Dino on 15/9/21. // Copyright (c) 2015年 daoshun. All rights reserved. // #import <UIKit/UIKit.h> @interface FirstViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *userName; @property (weak, nonatomic) IBOutlet UITextField *age; @property (weak, nonatomic) IBOutlet UITextField *sex; - (IBAction)valuePass:(id)sender; @end
//
// FirstViewController.m
// ValuePassByView
//
// Created by Dino on 15/9/21.
// Copyright (c) 2015年 daoshun. All rights reserved.
//
#import "FirstViewController.h"
#import "UserEntity.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)valuePass:(id)sender {
UserEntity *userEntity=[[UserEntity alloc] init];
userEntity.username=self.userName.text;
userEntity.age= [self.age.text intValue];
userEntity.sex=self.sex.text;
//
SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
secondViewController.userEntity=userEntity;
[self presentModalViewController:secondViewController animated:YES];
}
@end
2.第二个view的代码
// // SecondViewController.h // ValuePassByView // // Created by Dino on 15/9/21. // Copyright (c) 2015年 daoshun. All rights reserved. // #import <UIKit/UIKit.h> #import "UserEntity.h" @interface SecondViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *userName; @property (weak, nonatomic) IBOutlet UITextField *age; @property (weak, nonatomic) IBOutlet UITextField *sex; @property (retain,nonatomic) UserEntity *userEntity; - (IBAction)backToLastView:(id)sender; @end
//
// SecondViewController.m
// ValuePassByView
//
// Created by Dino on 15/9/21.
// Copyright (c) 2015年 daoshun. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize userName=_userName;
@synthesize age=_age;
@synthesize sex=_sex;
@synthesize userEntity=_userEntity;
- (void)viewDidLoad {
[super viewDidLoad];
self.userName.text=self.userEntity.username;
self.age.text=[NSString stringWithFormat:@"%d",self.userEntity.age];
self.sex.text=self.userEntity.sex;
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)backToLastView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
@end
3.新建一个类来传递数据
// // UserEntity.h // ValuePassByView // // Created by Dino on 15/9/21. // Copyright (c) 2015年 daoshun. All rights reserved. // #import <Foundation/Foundation.h> @interface UserEntity : NSObject @property (strong,nonatomic) NSString *username; @property (assign) int age; @property (strong,nonatomic) NSString *sex; @end
// // UserEntity.m // ValuePassByView // // Created by Dino on 15/9/21. // Copyright (c) 2015年 daoshun. All rights reserved. // #import "UserEntity.h" @implementation UserEntity @synthesize username=_username; @synthesize age=_age; @synthesize sex=_sex; @end
原文:http://my.oschina.net/u/1778933/blog/509003