如何在Xamarin中的视图控制器之间传递数据

时间:2017-01-27 15:39:00

标签: c# xamarin xamarin.ios

我正在尝试在第一个和第二个ViewController之间传递一些数据。 我已经像这样实例化了第二个视图控制器:

RegistrationViewController registration = new RegistrationViewController();

我将文本字段的值设置为来自注册类的变量电子邮件,如下所示:

registration.email = txtEmail.Text; 
this.NavigationController.PushViewController(registration, true);

然后在第二个ViewController我使用它:

public string email { get; set; }
txtEmail.Text = email ;

可悲的是,我在电子邮件变量中没有值。如何成功将数据传递到新的ViewController

更改控制器的代码:

 registration.email = txtEmail.Text;
    string password = txtPassword.Text;
    RegistrationViewController controller = this.Storyboard.InstantiateViewController("RegistrationViewController") as RegistrationViewController;

this.NavigationController.PushViewController(registration, true);
    this.NavigationController.PushViewController(controller, true);

2 个答案:

答案 0 :(得分:0)

基于此尝试一些事情:

SecondViewController controller = this.Storyboard.InstantiateViewController("SecondViewController") as SecondViewController;
//Here you pass the data from the registerViewController to the secondViewController
controller.email = registration.email;

this.NavigationController.PushViewController(registration, true);

希望有所帮助

答案 1 :(得分:0)

试试

  UIStoryboard board = UIStoryboard.FromName("Main", null);
  UIViewController ctrl = (UIViewController)board.InstantiateViewController("RegistrationViewController");
  ctrl.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
  this.PresentViewController(ctrl, true, null);