简体   繁体   中英

How to pass the data from modally presented ViewController to another ViewController in Xamarin.iOS?

I have to controller A and B.

From A I'm navigating to B in the B on button click I have open another ViewController using PresentModalViewController, which ask the user to enter some data. On the click of the submit button on that modally presented view I want to dismiss the modal and show the data on the B view that captured in modally presented view.

Here is what I have done:

I have InviteCollaboratorViewController from this I have modally presented AddNewCollaboratorDialogPopUpView and from there again I have modally presented another view named AddCollaboratorPopUpView.

AddCollaboratorPopUpView user will fill the details of and click on addCollaboratorButton, on this button click event I want to dismiss both the modally presented views, and want to reflects the changes in InviteCollaboratorViewController tableview.

InviteCollaboratorViewController

public partial class InviteCollaboratorViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        btnNewDialog.TouchUpInside += BtnNewDialog_TouchUpInside;

        MyCollaboratorsTableView.Source = new MyCollaboratorsTableViewSource(this, _listOfMyCollaborators);
        MyCollaboratorsTableView.RowHeight = UITableView.AutomaticDimension;
        MyCollaboratorsTableView.ReloadData();
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        MyCollaboratorsTableView.ReloadData();
    }

    public void BtnNewDialog_TouchUpInside(object sender, EventArgs e)
    {
        AddNewCollaboratorDialogPopUpView addNewCollaboratorDialogPop = this.Storyboard.InstantiateViewController("AddNewCollaboratorDialogPopUpView") as AddNewCollaboratorDialogPopUpView;
        if (addNewCollaboratorDialogPop != null)
        {
            addNewCollaboratorDialogPop.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
            addNewCollaboratorDialogPop.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
            this.PresentViewController(addNewCollaboratorDialogPop, true, null);
        }
    }
}

AddNewCollaboratorDialogPopUpView

public partial class AddNewCollaboratorDialogPopUpView : UIViewController
{
    public AddNewCollaboratorDialogPopUpView (IntPtr handle) : base (handle) { }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        SET_FONTS();
        SET_UI_CONTROLS_STYLING();


        createNewCollaboratorButton.TouchUpInside += CreateNewCollaboratorButton_TouchUpInside;

    }

    public void CreateNewCollaboratorButton_TouchUpInside(object sender, EventArgs e)
    {
        AddCollaboratorPopUpView addCollaboratorPopUp = this.Storyboard.InstantiateViewController("AddCollaboratorPopUpView") as AddCollaboratorPopUpView;
        if (addCollaboratorPopUp != null)
        {
            addCollaboratorPopUp.ModalPresentationStyle = UIModalPresentationStyle.Popover;
            addCollaboratorPopUp.ModalTransitionStyle = UIModalTransitionStyle.CrossDissolve;
            this.PresentViewController(addCollaboratorPopUp, true, null);
        }
    }
}

AddCollaboratorPopUpView

public partial class AddCollaboratorPopUpView : UIViewController
{
    public AddCollaboratorPopUpView (IntPtr handle) : base (handle) { }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        textFieldFirstName.ShouldEndEditing += TextFieldFirstName_ShouldEndEditing;
        textFieldLastName.ShouldEndEditing += TextFieldLastName_ShouldEndEditing;
        textFieldPhoneNumber.ShouldEndEditing += TextFieldPhoneNumber_ShouldEndEditing;
        textFieldEmail.ShouldEndEditing += TextFieldEmail_ShouldEndEditing;

        dismissPopupButton.TouchUpInside += DismissPopupButton_TouchUpInside;
        addCollaboratorButton.TouchUpInside += AddCollaboratorButton_TouchUpInside;
    }

    public void DismissPopupButton_TouchUpInside(object sender, EventArgs e)
    {
        PresentingViewController?.PresentingViewController?.DismissModalViewController(false);
    }

    public void AddCollaboratorButton_TouchUpInside(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textFieldFirstName.Text) && !string.IsNullOrEmpty(textFieldLastName.Text)
            && !string.IsNullOrEmpty(textFieldPhoneNumber.Text) && !string.IsNullOrEmpty(textFieldEmail.Text))
        {
            //Now from here what I want is to call the InviteCollaboratorViewController's ViewWillAppear() because that Refresh's the list of 
            //users which reflect the added user on list instantly as popup get dismissed.

            collaborator.FirstName = textFieldFirstName.Text;
            collaborator.LastName = textFieldLastName.Text;
            collaborator.Email = textFieldEmail.Text;
            collaborator.PhoneNumber = textFieldPhoneNumber.Text;
            GlobalConstants.listOfMyCollaborators.Add(collaborator);
            PresentingViewController.PresentingViewController.DismissModalViewController(false);

        }
        else
        {
            Toast.MakeToast("Please fill all required fields before submitting.").Show();
        }
    }
}

Try code below, I have made few modifications. It might help you

InviteCollaboratorViewController.cs

public override void ViewDidLoad()
{
    btnNewDialog.TouchUpInside += BtnNewDialog_TouchUpInside;
    _listOfMyCollaborators.Add(GlobalConstants.Collaborators);  
    GlobalConstants.Collaborators=null;
    MyCollaboratorsTableView.Source = new MyCollaboratorsTableViewSource(this, _listOfMyCollaborators);
    MyCollaboratorsTableView.RowHeight = UITableView.AutomaticDimension;
    MyCollaboratorsTableView.ReloadData();
}

AddCollaboratorPopUpView.cs

public void AddCollaboratorButton_TouchUpInside(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(textFieldFirstName.Text) && !string.IsNullOrEmpty(textFieldLastName.Text)
        && !string.IsNullOrEmpty(textFieldPhoneNumber.Text) && !string.IsNullOrEmpty(textFieldEmail.Text))
    {
        var objCollab=new Collaborators();      
        objCollab.FirstName = textFieldFirstName.Text;
        objCollab.LastName = textFieldLastName.Text;
        objCollab.Email = textFieldEmail.Text;
        objCollab.PhoneNumber = textFieldPhoneNumber.Text;      
        GlobalConstants.Collaborators=objCollab;
        PresentingViewController.PresentingViewController.DismissModalViewController(false);
    }
}

GlobalConstants.cs

Public class GlobalConstants
{
  public static Collaborators myCollab {get;set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM