如何将数据从模态呈现的ViewController传递到Xamarin.iOS中的另一个ViewController?

时间:2018-08-03 10:36:48

标签: ios xamarin xamarin.ios

我必须控制A和B。

从A导航到B的B单击按钮,我已使用PresentModalViewController打开另一个ViewController,要求用户输入一些数据。在该模式呈现的视图上单击“提交”按钮后,我想关闭该模态,并在以模式呈现的视图捕获的B视图上显示数据。

这是我所做的:

我从中获得了InviteCollaboratorViewController,我以模态展示了AddNewCollaboratorDialogPopUpView,从那以后,我又以模态展示了另一个名为AddCollaboratorPopUpView的视图。

AddCollaboratorPopUpView用户将填写的详细信息,然后单击addCollaboratorButton,在此按钮单击事件上,我想关闭两个模态显示的视图,并希望反映InviteCollaboratorViewController表视图中的更改。

  

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

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,我做了一些修改。可能会对您有帮助

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