1 使用SharedApplication,定义一个变量来传递.
2 使用文件plist,或者NSUserdefault来传递
3 通过一个单例的class来传递
4 通过Delegate来传递。
5 属性传值
6 数组,字典,数据库等
(1)AppDelegate.h
- #import <UIKit/UIKit.h>
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @property (strong, nonatomic) NSString *name;//定义一个NSString的
- @property (strong, nonatomic) NSString *str;//定义一个NSString的
- @end
(2) ViewController.h
- #import "AppDelegate.h"
- #import "OneViewController.h"
- @interface ViewController : UIViewController
- @property (weak, nonatomic) IBOutlet UITextField *nameTextField;
- @property (weak, nonatomic) IBOutlet UITextField *qqTextfield;//输入框
- - (IBAction)go:(id)sender;//go按钮
- @end
- - (IBAction)go:(id)sender {
- // 使用AppDelegate得到当前输入的数据
- AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
- appDelegate.str = self.qqTextfield.text ;
- appDelegate.name = self.nameTextField.text;
- // 进入下一页
- OneViewController *oneVC = [[OneViewController alloc]init];
- [self presentViewController:oneVC animated:YES completion:nil];
- }
(3) OneViewController.h
- #import <UIKit/UIKit.h>
- #import "AppDelegate.h"
- @interface OneViewController : UIViewController
- @property (weak, nonatomic) IBOutlet UITextField *aNameTextField;
- @property (weak, nonatomic) IBOutlet UITextField *oneTextField;
- @end
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- // 通过AppDelegate得到上一页输入的数据
- AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
- self.oneTextField.text = appdelegate.str;
- self.aNameTextField.text = appdelegate.name;
- }
效果图:
--
--