unbalanced
unbalanced

Reputation: 1202

How To Pass Value To Previous ViewContoller

I am developing an iOS app using Xamarin.IOS C#

I don't use segue for some reason.I have 3 UIViewContoller. First UIViewController push second UIViewController and after second contoller opens the third UIViewController. And when I select a row, I am going back to first controller. But at this time, in third controller I would like to change a variable of the first UIViewController.

logically it works like that

  1. Controller ---> 2. controller --> 3. controller-> select a row and ACCESS the first controller's variable and assign it --> 2. controller --> 1. controller and see this variable is change or not so this variable is null, not be changed

here is my code

THIS IS IN FIRST CONTROLLER

public int myVariable = 0;

-----
var controller2 =(SecondController)this.Storyboard.InstantiateViewController("SecondControllerStoryboard");
var mynextController = new SecondController(controller2.Handle);
NavigationController.PushViewController(mynextController, true);

THIS IS IN SECOND CONTROLLER

public int myVariable2=0;

// here i used segue

// it goes back to previous (first contoller)

NavigationController.PopViewControllerAnimated(true);

THIS IS IN THIRD CONTROLLER

var controller = (DocumentUpload)this.Storyboard.InstantiateViewController("FirstControllerStoryboard");
controller.myVariable= selectedIndex;

// It goes back the previous (second) controller
NavigationController.PopViewControllerAnimated(true);

so when first's controller viewdidapper, myVariable is still null

So probably, in third controller, it creates a new instance then i cant change the variable.

I am looking for like that desktop application

if (Application.OpenForms[i].Name)

...

Upvotes: 0

Views: 910

Answers (2)

JordanMazurke
JordanMazurke

Reputation: 1123

While the accepted answer will work, consider the practicality of this. If you require the persistence of data across multiple view controllers this really should be abstracted out into a separate data store.

Upvotes: 1

TheDutchDevil
TheDutchDevil

Reputation: 876

What you need to do is.

((Controller)NavigationController.ViewControllers[0]).myVariable = 3;

This works, because the navigation controller keeps an array of all the controllers that are currently stacked. You can use this to access controllers for views that are 'behind' the currently open view.

Upvotes: 3

Related Questions