当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Trying to reload my parentViewController's data but failing

问题描述:

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?

网友答案:

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];
网友答案:

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.

网友答案:

Try this:

MyParentControllerClass       *parent = (MyParentControllerClass *) self.parentViewController;
[parent.tableView reloadData];
分享给朋友:
您可能感兴趣的文章:
随机阅读: