how to send string value with popToViewController

2019-03-17 01:59发布

问题:

I am using navigationcontroller. i have (Root,A,B,C,D) class. i want to sand a string test value Class D to Class A via popToViewController.

Please give me suggestion.

Thanks

回答1:

UINavigationController maintain the list of all pushed controller in viewControllers and the root controller always reside at 0.

MyAController *myController = (MyAController *)[self.navigationController.viewControllers objectAtIndex:0];
myController.myText = @"My String" ;
[self.navigationController popToViewController:myController animated:YES];


回答2:

You might want to rethink your design, but since you haven't given enough information for me to suggest how, you could just try this:

A *aController = (A *)[myNavController rootViewController];
[aController setMyString:@"your string here"];
[myNavController popToRootViewControllerAnimated:YES];


回答3:

Make that string as property of class a then you need to access that object of the view A from navigation stack in class D.

And then access that property for using.

If Class A is rootView then jtbandes answers helps you otherwise pick it up from stack code some thing like this

if([self.navigationController.viewControllers count]>3)
             A *aController=(A *)[self.navigationController.viewControllers objectAtIndex: [self.navigationController.viewControllers count]-4];

if your navigation is A->B->c->D

then you can access c by [self.navigationController.viewControllers count]-2 similarly B by -3 A by -4.



回答4:

There are several approaches to achieve this.

  1. Using Notifications.
  2. Using Delegates.
  3. Using Outlets/Properties.


回答5:

You could also create a ControllerA instance var in your ControllerB and then pass the ControllerA (self) to ControllerB(ex: by a method) before call pushViewController from A to B. You can do the same from B to C...etc; i think the best solution is that of Jhaliya in case you want to work with navigationControllers.