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