如图新建一个viewController和DetailViewController
VC 和DetailVC 联线的Idetnifier 设置为:GoDetailVC
ViewController主要代码
- (IBAction)signInButtonTouched:(id)sender { [self performSegueWithIdentifier:@"GoDetailVC" sender:self]; } #pragma mark - prepare segue // A页面传值到B页面 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [super prepareForSegue:segue sender:sender]; if ([segue.identifier isEqualToString:@"GoDetailVC"]) { DetailViewController *detailVC = segue.destinationViewController; detailVC.passValue = self.usernameTextField.text; } } // B页面传值到A页面 -(IBAction)done:(UIStoryboardSegue *)segue{ if ([segue.sourceViewController isKindOfClass:[DetailViewController class]]) { DetailViewController * svc = (DetailViewController *)segue.sourceViewController; self.passwordTextField.text = svc.textField.text; } }
在StoryBoard中 DetailViewController的Done按钮 Action关联到 viewController的done方法 如图
1.在VC页面中第一个textField中输入值 点击 Sing in 按钮就会把值传到DetailViewController 的textField中显示出来。
2.在DetailViewController textField中修改值 点击Done 按钮就会把值回传到ViewController中
如果 是Present 方式要怎么传值勒。如图
用如下代码肯定传值行不通
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [super prepareForSegue:segue sender:sender]; if ([segue.identifier isEqualToString:@"GoDetailVC"]) { DetailViewController *detailVC = segue.destinationViewController; // 因为这取出来的是NavigationController 不是DetailViewController
detailVC.passValue = self.usernameTextField.text;
}
}
所以修改为如下:
// A页面传值到B页面 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { [super prepareForSegue:segue sender:sender]; if([[segue identifier] isEqualToString:@"GoDetailVC"]) { NSArray *viewControllerArray = [segue.destinationViewController viewControllers]; DetailViewController *detailVC = [viewControllerArray objectAtIndex:0]; detailVC.passValue = self.usernameTextField.text; } }