views:

128

answers:

3

Trying to [self.parentViewController.tableView reloadData] from a save: method in my DetailViewController but getting my favorite error: request for member 'tableView' in something not a structure or union.

Included the header for my RootViewController and casted the parentViewController as a RootViewController to no avail.

What have I botched?

A: 

Try:

[((MyParentViewControllerClassName *)self.parentViewController).tableView beginUpdates];
[((MyParentViewControllerClassName *)self.parentViewController).tableView reloadData];
[((MyParentViewControllerClassName *)self.parentViewController).tableView endUpdates];

replacing MyParentViewControllerClassName with your parent vc's class name.

To reference the parent view controller, I have always had to do something like this, e.g.:

NSArray *viewControllerArray = [self.navigationController viewControllers];
NSInteger parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

You could also try, following this example:

[[[viewControllerArray objectAtIndex:parentViewControllerIndex] tableView] beginUpdates];
[[[viewControllerArray objectAtIndex:parentViewControllerIndex] tableView] reloadData];
[[[viewControllerArray objectAtIndex:parentViewControllerIndex] tableView] endUpdates];
Alex Reynolds
A: 

Try this:

MyParentControllerClass       *parent = (MyParentControllerClass *) self.parentViewController;
[parent.tableView reloadData];
Ben Gottlieb
+2  A: 

Design wise, its a bad idea to have one controller sending messages directly to the views of another controller. Instead, you should have a method in the tableview's controller that other objects can call. This is will prevent you code from turning into spaghetti in which you never know what controller/object is changing the UI.

The reason the self.parentViewController.tableView doesn't work is that it's only defined as a generic UIViewController which has no tableView property. As noted above, casting it to the proper class will get rid of the message.

You can also use the [self.parentViewController tableview] call which doesn't produce the error because it treats the property like a method call.

TechZen