情况1:A页面跳转到B页面
方法1:在页面跳转的同时,将A页面的值带到B页面
B页面的控制器中设置一个接收值的对象,并且设置一个显示值的textfield,设置outlet
@property (nonatomic, copy) NSString *text; @property (weak, nonatomic) IBOutlet UITextField *textField2;
在B页面的控制器中的viewDidLoad将A页面传过来的值显示到textField上
- (void)viewDidLoad { [super viewDidLoad]; self.textField2.text = self.text; }
A页面的控制器中设置输入框以及跳转按钮
@property (weak, nonatomic) IBOutlet UITextField *textField; - (IBAction)btnClicked:(id)sender;
再给A页面的控制器中的按钮设置跳转事件
- (IBAction)btnClicked:(id)sender { SecondViewController *nc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"secondvc"]; nc.text = self.textField.text; [self.navigationController pushViewController:nc animated:YES]; }
方法2:通过segue传值
使用segue的performSegueWithIdentifier:sender:方法创建
[self performSegueWithIdentifier:@"secondVC" sender:self];
再通过- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender方法传值,将A页面的值带到B页面中去,在B页面的viewDidLoad方法中赋值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([segue.identifier compare:@"secondVC"]==NO) { id page2=segue.destinationViewController; [page2 setValue:self.textField.text forKey:@"value1"]; } }