views:

382

answers:

4

I have presented the viewController which contains a navigation controller whose view is loaded from another nib with the UITableView in it. I would like to dismiss that presented viewController from the UITableViewController. I have tried every combination of self.parentViewController and self.navigationController and self.navigationController.parentViewController but I am still not able to dismiss it. How and what is the best way for it to be dismissed?

A: 

I have never gotten self.navigationController.parentViewController to work as advertised, but this does work for me:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] myParentViewMethod:arg1 withArg2:arg2 ...];
Alex Reynolds
I could not get this to work, I only had one item in the array and that still did not dismiss the view. It may have to do with the way that I created it. I add a new UIViewController with XIB and create an outlet for a UINavigationController, I then hook that up. I next create a UITableViewController with XIB. Back in the UIViewController's XIB, I set it to load that new nib and set the class to the class of the UITableViewController.
woody993
A: 

Is this not working?

[myViewController presentModalViewController:otherController animated:YES];
// ...
[myViewController dismissModalViewControllerAnimated:YES];
slf
I need to process the data from the UITableView and am doing this inside the UITableViewController, I need to dismiss the parentViewController, but that does not work
woody993
A: 

It sounds like you've painted yourself into a corner a bit with your hierarchy, but that's ok I think we can fix it. There the two "usual" ways of doing this are:

  1. From controller A, present controller B, from B do work, from B dismiss, return to A
  2. From controller A, present controller B, from B do work, raise event via delegate protocol to A, from A dismiss B which returns control to A

In your current situation, this may require an uncomfortable ammount of refactoring, in which case it may be easier to just expose a handle to the other controller as a property, just make sure it's a weak reference like so:

@interface ControllerB : UIViewController
{
  ControllerA* controllerA;
}

@property (readwrite, nonatomic, assign) ControllerA* controllerA; // weak reference

@end

Or however you like, the point is to scope the variable properly so that it's available when you need it.

slf
A: 

I just ended up using delegates to dismiss the parent.

woody993