views:

686

answers:

3

Hi

I have a structure like this....

UITableViewController -> UITableViewCell -> UIView

I need the UIView to access a HashTable (NSMutableDictionary) in the UITableViewController

Is there a way to access the ViewController simply from the ViewCell (using [ViewCell superview] obviously won't work) ....Do I need to go down through the AppDelegate using [[UIApplication sharedApplication] delegate]?

Thanks!

+3  A: 

I usually maintain a weak reference from my UIView to my UIViewController if I need one, usually by creating a method something like this:

-(MyView*)initWithController:(CardCreatorViewController*) aController andFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame]) 
{

controller = aController;
    // more initialisation here
}
return self;
}

You could also use a delegate pattern if you want a more decoupled solution. I tend to think this is overkill for a view and its controller, but I would use it with a system of controllers and subcontrollers.

Jane Sales
+2  A: 

Since the UITableViewCell is somewhere in the view hierarchy, you can access your root view by retrieving view.superview until you get it. If you don't want to add any properties to your view, you can access its controller through the view's nextResponder property. You would have to cast it to whatever class you need, of course, and it may not be the cleanest use of the property. It's a quick-n-dirty hack to get to it.

If you're looking for something you can show your children though, I'd aim for going through your app delegate, or if your view controller happens to be a singleton, just implement the singleton design pattern and access it through that.

Ed Marty
+2  A: 

This should do the trick:

UITableView *tv = (UITableView *) self.superview.superview;
UITableViewController *vc = (UITableViewController *) tv.dataSource;
Kare Morstol