0

I need to send data to the parent view controller; so (in the parent) I created a method setLocWithName: loc: to do this. How do I create a object that is the parent controller? I tried parentViewController as well as presentingViewController (as shown below) but I'm getting the warning:

Incompatible pointer types initializing 'TDViewController *__strong' with an expression of type 'UIViewController *'

// set object of parent view controller
TDViewController *tDViewController = [[self navigationController] presentingViewController];

// get data
NSArray *locs = [[PLStore sharedStore] allLocs];
PL *setselectedLoc = [locs objectAtIndex:[indexpath row]];

// send data to parent view controller 
[tDViewController setLocWithName:[setselectedLoc pLocName] loc:[setselectedLoc loc1]];

// Pop this view controller off the stack
[self.navigationController popViewControllerAnimated:YES];
Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
FireSky
  • 181
  • 4
  • 17
  • What do you mean by "parent"? You need to describe what controllers you have, and how they relate to each other. – rdelmar May 01 '13 at 22:42
  • I have only known it as a parent child relationship, but I have a navigating controller (I'm not sure if the root view's controller is push on top of that or how that works but I digress.) Then the root view controller is the parent to TDViewcontroller (the "child" to the root view controller). TDViewController is pushed ontop of the root view controller and this PLViewController is the "child" to (or pushed onto of) TDViewController – FireSky May 01 '13 at 22:57
  • Well, that's not the right terminology, the controllers on the navigation stack don't have a parent-child relationship with each other (better thought of as siblings I guess). So, is your structure: UINavigationController --> rootVC --> TDViewController -->PLViewController? Which controller is the code you posted in, PLViewController? – rdelmar May 01 '13 at 23:20
  • you are right: RootVC the push TDVC on top, then push PLCV on that. The data and this code is in PLVC, DTVC has the method "setLocWithName loc" and needs the data. – FireSky May 01 '13 at 23:22

2 Answers2

1

Assuming presentingViewController (or parentViewController?) are actually of type TDViewController, you just need to do a cast on that first line:

TDViewController *tDViewController = (TDViewController *)[[self navigationController] presentingViewController];
joshuahealy
  • 3,529
  • 22
  • 29
  • Yeah I tried that. It didn't work either. does that mean that the view controller is not a type TDViewController? I created it so I'm assuming it is a TDViewController type. should I use it's parent UIViewController? – FireSky May 01 '13 at 22:25
  • You should try debugging your code, set a breakpoint on that line and see what the actual values are. – joshuahealy May 01 '13 at 22:29
  • I did and I took it to someone more experienced. First I was creating a TDViewController instead of allocating the parent. So the code would work until after I popped back to the parent and the information was not there. Now with the parentViewController as well as presentingViewController in the code the tDViewController returns a nil. – FireSky May 01 '13 at 22:35
  • You can check the type with `isKindOfClass:` or check if it responds to that selector with `respondsToSelector:` but agreeing w/ appclay on this one. – iwasrobbed May 01 '13 at 22:38
0

The correct way to do this is with delegation. TDViewController needs to make itself the delegate of PLViewController, which you should do just before pushing to PLViewController. PLViewController should declare a delegate protocol with a method in it that passes the two pieces of data that you need back in TDViewController.

To answer your actual question though, you can get a reference to TDViewController by getting the controller at index 1 on the navigation stack (in your particular case). This is really not a good way to do it, since you might change the order of controllers, or add or subtract controllers, but this is how you would do it:

TDViewController *tDViewController = (TDViewController *)[[self.navigationController viewControllers] objectAtIndex:1];
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Perfect! That worked like a charm, thanks. My data still not there so I'll look into the delegate thing (I really don't understand those at all), but at least the the memory locations match. What did you mean "in my particular case"? Is 0 the RootVC and 1 the TDVC making 2 the PLVC? – FireSky May 01 '13 at 23:44
  • @user2340928, yes, 0 the RootVC and 1 the TDVC making 2 the PLVC is what I meant by your particular case. – rdelmar May 02 '13 at 00:28
  • Nice the higher the VC is on the stack the bigger the number. Oh BTW thanks for the delegate tip I got it working. If anyone reading this needs a crash course on delegates I used [link](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – FireSky May 02 '13 at 00:42