How to pass data between view controllers in Xamar

2019-02-26 01:55发布

问题:

I am trying to pass some data between my first and second ViewController. I already instantiated the second view controller like this:

RegistrationViewController registration = new RegistrationViewController();

I set the value of the textfield to the variable email from the registration class like this:

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

Then in the second ViewController I use it like this:

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

Sadly, I don't have a value in the email variable. How can I achieve successfully passing the data to the new ViewController.

Code for changing the controller:

 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);

回答1:

Try something based on this :

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);

Hope it helps